blob: 88a057930b32ccb09a7585a028ed9f468a8383b2 [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)) {
78 ParmVarDecl *PVD = dyn_cast<ParmVarDecl>(VD);
79
80 // FIXME: generalize this for all decls.
81 if (PVD && PVD->getInvalidType())
82 return true;
Steve Naroff509fe022007-05-17 01:16:00 +000083 return new DeclRefExpr(VD, VD->getType(), Loc);
Steve Naroff7e6f7c22007-08-28 03:03:08 +000084 }
Steve Naroff46ba1eb2007-04-03 23:13:13 +000085 if (isa<TypedefDecl>(D))
Steve Narofff1e53692007-03-23 22:27:02 +000086 return Diag(Loc, diag::err_unexpected_typedef, II.getName());
87
88 assert(0 && "Invalid decl");
Chris Lattnerbb0ab462007-07-21 04:57:45 +000089 abort();
Chris Lattner17ed4872006-11-20 04:58:19 +000090}
Chris Lattnere168f762006-11-10 05:29:30 +000091
Anders Carlsson625bfc82007-07-21 05:21:51 +000092Sema::ExprResult Sema::ParsePreDefinedExpr(SourceLocation Loc,
93 tok::TokenKind Kind) {
94 PreDefinedExpr::IdentType IT;
95
Chris Lattnere168f762006-11-10 05:29:30 +000096 switch (Kind) {
97 default:
98 assert(0 && "Unknown simple primary expr!");
Chris Lattnere168f762006-11-10 05:29:30 +000099 case tok::kw___func__: // primary-expression: __func__ [C99 6.4.2.2]
Anders Carlsson625bfc82007-07-21 05:21:51 +0000100 IT = PreDefinedExpr::Func;
101 break;
Chris Lattnere168f762006-11-10 05:29:30 +0000102 case tok::kw___FUNCTION__: // primary-expression: __FUNCTION__ [GNU]
Anders Carlsson625bfc82007-07-21 05:21:51 +0000103 IT = PreDefinedExpr::Function;
104 break;
Chris Lattnere168f762006-11-10 05:29:30 +0000105 case tok::kw___PRETTY_FUNCTION__: // primary-expression: __P..Y_F..N__ [GNU]
Anders Carlsson625bfc82007-07-21 05:21:51 +0000106 IT = PreDefinedExpr::PrettyFunction;
107 break;
Chris Lattnere168f762006-11-10 05:29:30 +0000108 }
Anders Carlsson625bfc82007-07-21 05:21:51 +0000109
110 // Pre-defined identifiers are always of type char *.
111 return new PreDefinedExpr(Loc, Context.getPointerType(Context.CharTy), IT);
Chris Lattnere168f762006-11-10 05:29:30 +0000112}
113
Chris Lattner146762e2007-07-20 16:59:19 +0000114Sema::ExprResult Sema::ParseCharacterConstant(const Token &Tok) {
Chris Lattner23b7eb62007-06-15 23:05:46 +0000115 llvm::SmallString<16> CharBuffer;
Steve Naroffae4143e2007-04-26 20:39:23 +0000116 CharBuffer.resize(Tok.getLength());
117 const char *ThisTokBegin = &CharBuffer[0];
118 unsigned ActualLength = PP.getSpelling(Tok, ThisTokBegin);
119
120 CharLiteralParser Literal(ThisTokBegin, ThisTokBegin+ActualLength,
121 Tok.getLocation(), PP);
122 if (Literal.hadError())
123 return ExprResult(true);
Steve Naroff509fe022007-05-17 01:16:00 +0000124 return new CharacterLiteral(Literal.getValue(), Context.IntTy,
125 Tok.getLocation());
Steve Naroffae4143e2007-04-26 20:39:23 +0000126}
127
Chris Lattner146762e2007-07-20 16:59:19 +0000128Action::ExprResult Sema::ParseNumericConstant(const Token &Tok) {
Steve Narofff2fb89e2007-03-13 20:29:44 +0000129 // fast path for a single digit (which is quite common). A single digit
130 // cannot have a trigraph, escaped newline, radix prefix, or type suffix.
131 if (Tok.getLength() == 1) {
132 const char *t = PP.getSourceManager().getCharacterData(Tok.getLocation());
Chris Lattner67ca9252007-05-21 01:08:44 +0000133
Chris Lattner4481b422007-07-14 01:29:45 +0000134 unsigned IntSize = Context.getTypeSize(Context.IntTy, Tok.getLocation());
Chris Lattner23b7eb62007-06-15 23:05:46 +0000135 return ExprResult(new IntegerLiteral(llvm::APInt(IntSize, *t-'0'),
136 Context.IntTy,
Steve Naroff509fe022007-05-17 01:16:00 +0000137 Tok.getLocation()));
Steve Narofff2fb89e2007-03-13 20:29:44 +0000138 }
Chris Lattner23b7eb62007-06-15 23:05:46 +0000139 llvm::SmallString<512> IntegerBuffer;
Steve Naroff8160ea22007-03-06 01:09:46 +0000140 IntegerBuffer.resize(Tok.getLength());
141 const char *ThisTokBegin = &IntegerBuffer[0];
142
Chris Lattner67ca9252007-05-21 01:08:44 +0000143 // Get the spelling of the token, which eliminates trigraphs, etc.
Steve Naroff8160ea22007-03-06 01:09:46 +0000144 unsigned ActualLength = PP.getSpelling(Tok, ThisTokBegin);
Steve Naroff09ef4742007-03-09 23:16:33 +0000145 NumericLiteralParser Literal(ThisTokBegin, ThisTokBegin+ActualLength,
Steve Naroff451d8f162007-03-12 23:22:38 +0000146 Tok.getLocation(), PP);
Steve Narofff2fb89e2007-03-13 20:29:44 +0000147 if (Literal.hadError)
148 return ExprResult(true);
Chris Lattner67ca9252007-05-21 01:08:44 +0000149
Chris Lattner1c20a172007-08-26 03:42:43 +0000150 Expr *Res;
151
152 if (Literal.isFloatingLiteral()) {
153 // FIXME: handle float values > 32 (including compute the real type...).
154 QualType Ty = Literal.isFloat ? Context.FloatTy : Context.DoubleTy;
155 Res = new FloatingLiteral(Literal.GetFloatValue(), Ty, Tok.getLocation());
156 } else if (!Literal.isIntegerLiteral()) {
157 return ExprResult(true);
158 } else {
Steve Naroffe5aa9be2007-04-05 22:36:20 +0000159 QualType t;
Chris Lattner67ca9252007-05-21 01:08:44 +0000160
161 // Get the value in the widest-possible width.
Chris Lattner23b7eb62007-06-15 23:05:46 +0000162 llvm::APInt ResultVal(Context.Target.getIntMaxTWidth(Tok.getLocation()), 0);
Chris Lattner67ca9252007-05-21 01:08:44 +0000163
164 if (Literal.GetIntegerValue(ResultVal)) {
165 // If this value didn't fit into uintmax_t, warn and force to ull.
166 Diag(Tok.getLocation(), diag::warn_integer_too_large);
167 t = Context.UnsignedLongLongTy;
Chris Lattner4481b422007-07-14 01:29:45 +0000168 assert(Context.getTypeSize(t, Tok.getLocation()) ==
Chris Lattner67ca9252007-05-21 01:08:44 +0000169 ResultVal.getBitWidth() && "long long is not intmax_t?");
Steve Naroff09ef4742007-03-09 23:16:33 +0000170 } else {
Chris Lattner67ca9252007-05-21 01:08:44 +0000171 // If this value fits into a ULL, try to figure out what else it fits into
172 // according to the rules of C99 6.4.4.1p5.
173
174 // Octal, Hexadecimal, and integers with a U suffix are allowed to
175 // be an unsigned int.
176 bool AllowUnsigned = Literal.isUnsigned || Literal.getRadix() != 10;
177
178 // Check from smallest to largest, picking the smallest type we can.
Chris Lattner7b939cf2007-08-23 21:58:08 +0000179 if (!Literal.isLong && !Literal.isLongLong) {
180 // Are int/unsigned possibilities?
Chris Lattner4481b422007-07-14 01:29:45 +0000181 unsigned IntSize = Context.getTypeSize(Context.IntTy,Tok.getLocation());
Chris Lattner67ca9252007-05-21 01:08:44 +0000182 // Does it fit in a unsigned int?
183 if (ResultVal.isIntN(IntSize)) {
184 // Does it fit in a signed int?
185 if (!Literal.isUnsigned && ResultVal[IntSize-1] == 0)
186 t = Context.IntTy;
187 else if (AllowUnsigned)
188 t = Context.UnsignedIntTy;
189 }
190
191 if (!t.isNull())
192 ResultVal.trunc(IntSize);
193 }
194
195 // Are long/unsigned long possibilities?
196 if (t.isNull() && !Literal.isLongLong) {
Chris Lattner4481b422007-07-14 01:29:45 +0000197 unsigned LongSize = Context.getTypeSize(Context.LongTy,
198 Tok.getLocation());
Chris Lattner67ca9252007-05-21 01:08:44 +0000199
200 // Does it fit in a unsigned long?
201 if (ResultVal.isIntN(LongSize)) {
202 // Does it fit in a signed long?
203 if (!Literal.isUnsigned && ResultVal[LongSize-1] == 0)
204 t = Context.LongTy;
205 else if (AllowUnsigned)
206 t = Context.UnsignedLongTy;
207 }
208 if (!t.isNull())
209 ResultVal.trunc(LongSize);
210 }
211
212 // Finally, check long long if needed.
213 if (t.isNull()) {
214 unsigned LongLongSize =
Chris Lattner4481b422007-07-14 01:29:45 +0000215 Context.getTypeSize(Context.LongLongTy, Tok.getLocation());
Chris Lattner67ca9252007-05-21 01:08:44 +0000216
217 // Does it fit in a unsigned long long?
218 if (ResultVal.isIntN(LongLongSize)) {
219 // Does it fit in a signed long long?
220 if (!Literal.isUnsigned && ResultVal[LongLongSize-1] == 0)
221 t = Context.LongLongTy;
222 else if (AllowUnsigned)
223 t = Context.UnsignedLongLongTy;
224 }
225 }
226
227 // If we still couldn't decide a type, we probably have something that
228 // does not fit in a signed long long, but has no U suffix.
229 if (t.isNull()) {
230 Diag(Tok.getLocation(), diag::warn_integer_too_large_for_signed);
231 t = Context.UnsignedLongLongTy;
232 }
Steve Naroff09ef4742007-03-09 23:16:33 +0000233 }
Chris Lattner67ca9252007-05-21 01:08:44 +0000234
Chris Lattner1c20a172007-08-26 03:42:43 +0000235 Res = new IntegerLiteral(ResultVal, t, Tok.getLocation());
Steve Naroff09ef4742007-03-09 23:16:33 +0000236 }
Chris Lattner1c20a172007-08-26 03:42:43 +0000237
238 // If this is an imaginary literal, create the ImaginaryLiteral wrapper.
239 if (Literal.isImaginary)
240 Res = new ImaginaryLiteral(Res, Context.getComplexType(Res->getType()));
241
242 return Res;
Chris Lattnere168f762006-11-10 05:29:30 +0000243}
244
245Action::ExprResult Sema::ParseParenExpr(SourceLocation L, SourceLocation R,
246 ExprTy *Val) {
Steve Naroffae4143e2007-04-26 20:39:23 +0000247 Expr *e = (Expr *)Val;
248 assert((e != 0) && "ParseParenExpr() missing expr");
Steve Naroff53f07dc2007-05-17 21:49:33 +0000249 return new ParenExpr(L, R, e);
Chris Lattnere168f762006-11-10 05:29:30 +0000250}
251
Steve Naroff71b59a92007-06-04 22:22:31 +0000252/// The UsualUnaryConversions() function is *not* called by this routine.
Steve Naroffa78fe7e2007-05-16 19:47:19 +0000253/// See C99 6.3.2.1p[2-4] for more details.
Steve Naroff043d45d2007-05-15 02:32:35 +0000254QualType Sema::CheckSizeOfAlignOfOperand(QualType exprType,
255 SourceLocation OpLoc, bool isSizeof) {
256 // C99 6.5.3.4p1:
257 if (isa<FunctionType>(exprType) && isSizeof)
258 // alignof(function) is allowed.
259 Diag(OpLoc, diag::ext_sizeof_function_type);
260 else if (exprType->isVoidType())
261 Diag(OpLoc, diag::ext_sizeof_void_type, isSizeof ? "sizeof" : "__alignof");
262 else if (exprType->isIncompleteType()) {
Steve Naroff043d45d2007-05-15 02:32:35 +0000263 Diag(OpLoc, isSizeof ? diag::err_sizeof_incomplete_type :
Chris Lattner3dc3d772007-05-16 18:07:12 +0000264 diag::err_alignof_incomplete_type,
265 exprType.getAsString());
Steve Naroff043d45d2007-05-15 02:32:35 +0000266 return QualType(); // error
267 }
268 // C99 6.5.3.4p4: the type (an unsigned integer type) is size_t.
269 return Context.getSizeType();
270}
271
Chris Lattnere168f762006-11-10 05:29:30 +0000272Action::ExprResult Sema::
273ParseSizeOfAlignOfTypeExpr(SourceLocation OpLoc, bool isSizeof,
Steve Naroff509fe022007-05-17 01:16:00 +0000274 SourceLocation LPLoc, TypeTy *Ty,
275 SourceLocation RPLoc) {
Chris Lattner0d8b1a12006-11-20 04:34:45 +0000276 // If error parsing type, ignore.
277 if (Ty == 0) return true;
Chris Lattner6531c102007-01-23 22:29:49 +0000278
279 // Verify that this is a valid expression.
Steve Naroffe5aa9be2007-04-05 22:36:20 +0000280 QualType ArgTy = QualType::getFromOpaquePtr(Ty);
Chris Lattner6531c102007-01-23 22:29:49 +0000281
Steve Naroff043d45d2007-05-15 02:32:35 +0000282 QualType resultType = CheckSizeOfAlignOfOperand(ArgTy, OpLoc, isSizeof);
283
284 if (resultType.isNull())
285 return true;
Steve Naroff509fe022007-05-17 01:16:00 +0000286 return new SizeOfAlignOfTypeExpr(isSizeof, ArgTy, resultType, OpLoc, RPLoc);
Chris Lattnere168f762006-11-10 05:29:30 +0000287}
288
Chris Lattner74ed76b2007-08-24 21:41:10 +0000289QualType Sema::CheckRealImagOperand(Expr *&V, SourceLocation Loc) {
Chris Lattner30b5dd02007-08-24 21:16:53 +0000290 DefaultFunctionArrayConversion(V);
291
Chris Lattnere267f5d2007-08-26 05:39:26 +0000292 // These operators return the element type of a complex type.
Chris Lattner30b5dd02007-08-24 21:16:53 +0000293 if (const ComplexType *CT = V->getType()->getAsComplexType())
294 return CT->getElementType();
Chris Lattnere267f5d2007-08-26 05:39:26 +0000295
296 // Otherwise they pass through real integer and floating point types here.
297 if (V->getType()->isArithmeticType())
298 return V->getType();
299
300 // Reject anything else.
301 Diag(Loc, diag::err_realimag_invalid_type, V->getType().getAsString());
302 return QualType();
Chris Lattner30b5dd02007-08-24 21:16:53 +0000303}
304
305
Chris Lattnere168f762006-11-10 05:29:30 +0000306
307Action::ExprResult Sema::ParsePostfixUnaryOp(SourceLocation OpLoc,
308 tok::TokenKind Kind,
309 ExprTy *Input) {
310 UnaryOperator::Opcode Opc;
311 switch (Kind) {
312 default: assert(0 && "Unknown unary op!");
313 case tok::plusplus: Opc = UnaryOperator::PostInc; break;
314 case tok::minusminus: Opc = UnaryOperator::PostDec; break;
315 }
Steve Naroff71ce2e02007-05-18 22:53:50 +0000316 QualType result = CheckIncrementDecrementOperand((Expr *)Input, OpLoc);
Steve Naroff35d85152007-05-07 00:24:15 +0000317 if (result.isNull())
318 return true;
Steve Naroff509fe022007-05-17 01:16:00 +0000319 return new UnaryOperator((Expr *)Input, Opc, result, OpLoc);
Chris Lattnere168f762006-11-10 05:29:30 +0000320}
321
322Action::ExprResult Sema::
323ParseArraySubscriptExpr(ExprTy *Base, SourceLocation LLoc,
324 ExprTy *Idx, SourceLocation RLoc) {
Chris Lattner5981db42007-07-15 23:59:53 +0000325 Expr *LHSExp = static_cast<Expr*>(Base), *RHSExp = static_cast<Expr*>(Idx);
Chris Lattner36d572b2007-07-16 00:14:47 +0000326
327 // Perform default conversions.
328 DefaultFunctionArrayConversion(LHSExp);
329 DefaultFunctionArrayConversion(RHSExp);
Chris Lattner5981db42007-07-15 23:59:53 +0000330
Chris Lattner36d572b2007-07-16 00:14:47 +0000331 QualType LHSTy = LHSExp->getType(), RHSTy = RHSExp->getType();
Steve Narofff1e53692007-03-23 22:27:02 +0000332
Steve Naroffc1aadb12007-03-28 21:49:40 +0000333 // C99 6.5.2.1p2: the expression e1[e2] is by definition precisely equivalent
334 // to the expression *((e1)+(e2)). This means the array "Base" may actually be
Steve Narofff1e53692007-03-23 22:27:02 +0000335 // in the subscript position. As a result, we need to derive the array base
336 // and index from the expression types.
Chris Lattner36d572b2007-07-16 00:14:47 +0000337 Expr *BaseExpr, *IndexExpr;
338 QualType ResultType;
Chris Lattnerc996b172007-07-31 16:53:04 +0000339 if (const PointerType *PTy = LHSTy->getAsPointerType()) {
Chris Lattner36d572b2007-07-16 00:14:47 +0000340 BaseExpr = LHSExp;
341 IndexExpr = RHSExp;
342 // FIXME: need to deal with const...
343 ResultType = PTy->getPointeeType();
Chris Lattnerc996b172007-07-31 16:53:04 +0000344 } else if (const PointerType *PTy = RHSTy->getAsPointerType()) {
Chris Lattneraee0cfd2007-07-16 00:23:25 +0000345 // Handle the uncommon case of "123[Ptr]".
Chris Lattner36d572b2007-07-16 00:14:47 +0000346 BaseExpr = RHSExp;
347 IndexExpr = LHSExp;
348 // FIXME: need to deal with const...
349 ResultType = PTy->getPointeeType();
Chris Lattner41977962007-07-31 19:29:30 +0000350 } else if (const VectorType *VTy = LHSTy->getAsVectorType()) {
351 BaseExpr = LHSExp; // vectors: V[123]
Chris Lattner36d572b2007-07-16 00:14:47 +0000352 IndexExpr = RHSExp;
Steve Naroff01047312007-08-03 22:40:33 +0000353
354 // Component access limited to variables (reject vec4.rg[1]).
355 if (!isa<DeclRefExpr>(BaseExpr))
356 return Diag(LLoc, diag::err_ocuvector_component_access,
357 SourceRange(LLoc, RLoc));
Chris Lattner36d572b2007-07-16 00:14:47 +0000358 // FIXME: need to deal with const...
359 ResultType = VTy->getElementType();
Steve Naroffb3096442007-06-09 03:47:53 +0000360 } else {
Chris Lattner5981db42007-07-15 23:59:53 +0000361 return Diag(LHSExp->getLocStart(), diag::err_typecheck_subscript_value,
362 RHSExp->getSourceRange());
Steve Naroffb3096442007-06-09 03:47:53 +0000363 }
Steve Naroffc1aadb12007-03-28 21:49:40 +0000364 // C99 6.5.2.1p1
Chris Lattner36d572b2007-07-16 00:14:47 +0000365 if (!IndexExpr->getType()->isIntegerType())
366 return Diag(IndexExpr->getLocStart(), diag::err_typecheck_subscript,
367 IndexExpr->getSourceRange());
Steve Naroffb29cdd52007-07-10 18:23:31 +0000368
Chris Lattner36d572b2007-07-16 00:14:47 +0000369 // C99 6.5.2.1p1: "shall have type "pointer to *object* type". In practice,
370 // the following check catches trying to index a pointer to a function (e.g.
371 // void (*)(int)). Functions are not objects in C99.
372 if (!ResultType->isObjectType())
373 return Diag(BaseExpr->getLocStart(),
374 diag::err_typecheck_subscript_not_object,
375 BaseExpr->getType().getAsString(), BaseExpr->getSourceRange());
376
377 return new ArraySubscriptExpr(LHSExp, RHSExp, ResultType, RLoc);
Chris Lattnere168f762006-11-10 05:29:30 +0000378}
379
Steve Narofff8fd09e2007-07-27 22:15:19 +0000380QualType Sema::
381CheckOCUVectorComponent(QualType baseType, SourceLocation OpLoc,
382 IdentifierInfo &CompName, SourceLocation CompLoc) {
Chris Lattner41977962007-07-31 19:29:30 +0000383 const OCUVectorType *vecType = baseType->getAsOCUVectorType();
Steve Narofff8fd09e2007-07-27 22:15:19 +0000384
385 // The vector accessor can't exceed the number of elements.
386 const char *compStr = CompName.getName();
387 if (strlen(compStr) > vecType->getNumElements()) {
388 Diag(OpLoc, diag::err_ocuvector_component_exceeds_length,
389 baseType.getAsString(), SourceRange(CompLoc));
390 return QualType();
391 }
392 // The component names must come from the same set.
Chris Lattner7e152db2007-08-02 22:33:49 +0000393 if (vecType->getPointAccessorIdx(*compStr) != -1) {
394 do
395 compStr++;
396 while (*compStr && vecType->getPointAccessorIdx(*compStr) != -1);
397 } else if (vecType->getColorAccessorIdx(*compStr) != -1) {
398 do
399 compStr++;
400 while (*compStr && vecType->getColorAccessorIdx(*compStr) != -1);
401 } else if (vecType->getTextureAccessorIdx(*compStr) != -1) {
402 do
403 compStr++;
404 while (*compStr && vecType->getTextureAccessorIdx(*compStr) != -1);
405 }
Steve Narofff8fd09e2007-07-27 22:15:19 +0000406
407 if (*compStr) {
408 // We didn't get to the end of the string. This means the component names
409 // didn't come from the same set *or* we encountered an illegal name.
410 Diag(OpLoc, diag::err_ocuvector_component_name_illegal,
411 std::string(compStr,compStr+1), SourceRange(CompLoc));
412 return QualType();
413 }
414 // Each component accessor can't exceed the vector type.
415 compStr = CompName.getName();
416 while (*compStr) {
417 if (vecType->isAccessorWithinNumElements(*compStr))
418 compStr++;
419 else
420 break;
421 }
422 if (*compStr) {
423 // We didn't get to the end of the string. This means a component accessor
424 // exceeds the number of elements in the vector.
425 Diag(OpLoc, diag::err_ocuvector_component_exceeds_length,
426 baseType.getAsString(), SourceRange(CompLoc));
427 return QualType();
428 }
429 // The component accessor looks fine - now we need to compute the actual type.
430 // The vector type is implied by the component accessor. For example,
431 // vec4.b is a float, vec4.xy is a vec2, vec4.rgb is a vec3, etc.
432 unsigned CompSize = strlen(CompName.getName());
433 if (CompSize == 1)
434 return vecType->getElementType();
Steve Naroffddf5a1d2007-07-29 16:33:31 +0000435
436 QualType VT = Context.getOCUVectorType(vecType->getElementType(), CompSize);
437 // Now look up the TypeDefDecl from the vector type. Without this,
438 // diagostics look bad. We want OCU vector types to appear built-in.
439 for (unsigned i = 0, e = OCUVectorDecls.size(); i != e; ++i) {
440 if (OCUVectorDecls[i]->getUnderlyingType() == VT)
441 return Context.getTypedefType(OCUVectorDecls[i]);
442 }
443 return VT; // should never get here (a typedef type should always be found).
Steve Narofff8fd09e2007-07-27 22:15:19 +0000444}
445
Chris Lattnere168f762006-11-10 05:29:30 +0000446Action::ExprResult Sema::
447ParseMemberReferenceExpr(ExprTy *Base, SourceLocation OpLoc,
448 tok::TokenKind OpKind, SourceLocation MemberLoc,
449 IdentifierInfo &Member) {
Steve Naroff185616f2007-07-26 03:11:44 +0000450 Expr *BaseExpr = static_cast<Expr *>(Base);
451 assert(BaseExpr && "no record expression");
Steve Naroffca8f7122007-04-01 01:41:35 +0000452
Steve Naroff185616f2007-07-26 03:11:44 +0000453 QualType BaseType = BaseExpr->getType();
454 assert(!BaseType.isNull() && "no type for member expression");
Steve Naroffca8f7122007-04-01 01:41:35 +0000455
Steve Narofff1e53692007-03-23 22:27:02 +0000456 if (OpKind == tok::arrow) {
Chris Lattnerc996b172007-07-31 16:53:04 +0000457 if (const PointerType *PT = BaseType->getAsPointerType())
Steve Naroff185616f2007-07-26 03:11:44 +0000458 BaseType = PT->getPointeeType();
459 else
460 return Diag(OpLoc, diag::err_typecheck_member_reference_arrow,
461 SourceRange(MemberLoc));
Steve Narofff1e53692007-03-23 22:27:02 +0000462 }
Steve Narofff8fd09e2007-07-27 22:15:19 +0000463 // The base type is either a record or an OCUVectorType.
Chris Lattner41977962007-07-31 19:29:30 +0000464 if (const RecordType *RTy = BaseType->getAsRecordType()) {
Steve Naroff185616f2007-07-26 03:11:44 +0000465 RecordDecl *RDecl = RTy->getDecl();
466 if (RTy->isIncompleteType())
467 return Diag(OpLoc, diag::err_typecheck_incomplete_tag, RDecl->getName(),
468 BaseExpr->getSourceRange());
469 // The record definition is complete, now make sure the member is valid.
Steve Narofff8fd09e2007-07-27 22:15:19 +0000470 FieldDecl *MemberDecl = RDecl->getMember(&Member);
471 if (!MemberDecl)
Steve Naroff185616f2007-07-26 03:11:44 +0000472 return Diag(OpLoc, diag::err_typecheck_no_member, Member.getName(),
473 SourceRange(MemberLoc));
Steve Narofff8fd09e2007-07-27 22:15:19 +0000474 return new MemberExpr(BaseExpr, OpKind==tok::arrow, MemberDecl, MemberLoc);
475 } else if (BaseType->isOCUVectorType() && OpKind == tok::period) {
Steve Naroff01047312007-08-03 22:40:33 +0000476 // Component access limited to variables (reject vec4.rg.g).
477 if (!isa<DeclRefExpr>(BaseExpr))
478 return Diag(OpLoc, diag::err_ocuvector_component_access,
479 SourceRange(MemberLoc));
Steve Narofff8fd09e2007-07-27 22:15:19 +0000480 QualType ret = CheckOCUVectorComponent(BaseType, OpLoc, Member, MemberLoc);
481 if (ret.isNull())
482 return true;
Chris Lattnerd268a7a2007-08-03 17:31:20 +0000483 return new OCUVectorElementExpr(ret, BaseExpr, Member, MemberLoc);
Steve Naroff185616f2007-07-26 03:11:44 +0000484 } else
485 return Diag(OpLoc, diag::err_typecheck_member_reference_structUnion,
486 SourceRange(MemberLoc));
Chris Lattnere168f762006-11-10 05:29:30 +0000487}
488
489/// ParseCallExpr - Handle a call to Fn with the specified array of arguments.
490/// This provides the location of the left/right parens and a list of comma
491/// locations.
492Action::ExprResult Sema::
Chris Lattner38dbdb22007-07-21 03:03:59 +0000493ParseCallExpr(ExprTy *fn, SourceLocation LParenLoc,
494 ExprTy **args, unsigned NumArgsInCall,
Chris Lattnere168f762006-11-10 05:29:30 +0000495 SourceLocation *CommaLocs, SourceLocation RParenLoc) {
Chris Lattner38dbdb22007-07-21 03:03:59 +0000496 Expr *Fn = static_cast<Expr *>(fn);
497 Expr **Args = reinterpret_cast<Expr**>(args);
498 assert(Fn && "no function call expression");
Steve Naroff8563f652007-05-28 19:25:56 +0000499
Chris Lattner38dbdb22007-07-21 03:03:59 +0000500 UsualUnaryConversions(Fn);
501 QualType funcType = Fn->getType();
Steve Naroffae4143e2007-04-26 20:39:23 +0000502
Chris Lattner3d01e4e2007-06-06 05:14:05 +0000503 // C99 6.5.2.2p1 - "The expression that denotes the called function shall have
504 // type pointer to function".
Chris Lattnere5a6cbd2007-07-31 21:27:01 +0000505 const PointerType *PT = funcType->getAsPointerType();
Chris Lattner3d01e4e2007-06-06 05:14:05 +0000506 if (PT == 0)
Chris Lattner38dbdb22007-07-21 03:03:59 +0000507 return Diag(Fn->getLocStart(), diag::err_typecheck_call_not_function,
508 SourceRange(Fn->getLocStart(), RParenLoc));
Chris Lattner3343f812007-06-06 05:05:41 +0000509
Chris Lattnere5a6cbd2007-07-31 21:27:01 +0000510 const FunctionType *funcT = PT->getPointeeType()->getAsFunctionType();
Chris Lattner3d01e4e2007-06-06 05:14:05 +0000511 if (funcT == 0)
Chris Lattner38dbdb22007-07-21 03:03:59 +0000512 return Diag(Fn->getLocStart(), diag::err_typecheck_call_not_function,
513 SourceRange(Fn->getLocStart(), RParenLoc));
Steve Naroffb8c289d2007-05-08 22:18:00 +0000514
Steve Naroff17f76e02007-05-03 21:03:48 +0000515 // If a prototype isn't declared, the parser implicitly defines a func decl
516 QualType resultType = funcT->getResultType();
517
518 if (const FunctionTypeProto *proto = dyn_cast<FunctionTypeProto>(funcT)) {
519 // C99 6.5.2.2p7 - the arguments are implicitly converted, as if by
520 // assignment, to the types of the corresponding parameter, ...
521
522 unsigned NumArgsInProto = proto->getNumArgs();
Steve Naroffb8c289d2007-05-08 22:18:00 +0000523 unsigned NumArgsToCheck = NumArgsInCall;
Steve Naroff17f76e02007-05-03 21:03:48 +0000524
Steve Naroffb8c289d2007-05-08 22:18:00 +0000525 if (NumArgsInCall < NumArgsInProto)
Steve Naroff56faab22007-05-30 04:20:12 +0000526 Diag(RParenLoc, diag::err_typecheck_call_too_few_args,
Chris Lattner38dbdb22007-07-21 03:03:59 +0000527 Fn->getSourceRange());
Steve Naroffb8c289d2007-05-08 22:18:00 +0000528 else if (NumArgsInCall > NumArgsInProto) {
Steve Naroff8563f652007-05-28 19:25:56 +0000529 if (!proto->isVariadic()) {
Chris Lattnera6f5ab52007-07-21 03:09:58 +0000530 Diag(Args[NumArgsInProto]->getLocStart(),
Chris Lattner38dbdb22007-07-21 03:03:59 +0000531 diag::err_typecheck_call_too_many_args, Fn->getSourceRange(),
Chris Lattnera6f5ab52007-07-21 03:09:58 +0000532 SourceRange(Args[NumArgsInProto]->getLocStart(),
533 Args[NumArgsInCall-1]->getLocEnd()));
Steve Naroff8563f652007-05-28 19:25:56 +0000534 }
Steve Naroffb8c289d2007-05-08 22:18:00 +0000535 NumArgsToCheck = NumArgsInProto;
Steve Naroff17f76e02007-05-03 21:03:48 +0000536 }
537 // Continue to check argument types (even if we have too few/many args).
Steve Naroffb8c289d2007-05-08 22:18:00 +0000538 for (unsigned i = 0; i < NumArgsToCheck; i++) {
Chris Lattner38dbdb22007-07-21 03:03:59 +0000539 Expr *argExpr = Args[i];
Steve Naroff8563f652007-05-28 19:25:56 +0000540 assert(argExpr && "ParseCallExpr(): missing argument expression");
541
Steve Naroff17f76e02007-05-03 21:03:48 +0000542 QualType lhsType = proto->getArgType(i);
Steve Naroff8563f652007-05-28 19:25:56 +0000543 QualType rhsType = argExpr->getType();
Steve Naroff44fd8ff2007-07-24 21:46:40 +0000544
Steve Naroffb8af1c22007-07-25 20:45:33 +0000545 // If necessary, apply function/array conversion. C99 6.7.5.3p[7,8].
Chris Lattner41977962007-07-31 19:29:30 +0000546 if (const ArrayType *ary = lhsType->getAsArrayType())
Steve Naroff44fd8ff2007-07-24 21:46:40 +0000547 lhsType = Context.getPointerType(ary->getElementType());
Steve Naroffb8af1c22007-07-25 20:45:33 +0000548 else if (lhsType->isFunctionType())
Steve Naroff44fd8ff2007-07-24 21:46:40 +0000549 lhsType = Context.getPointerType(lhsType);
550
Steve Naroffb8ea4fb2007-07-13 23:32:42 +0000551 AssignmentCheckResult result = CheckSingleAssignmentConstraints(lhsType,
552 argExpr);
Steve Naroff0c1c7ed2007-08-24 22:33:52 +0000553 if (Args[i] != argExpr) // The expression was converted.
554 Args[i] = argExpr; // Make sure we store the converted expression.
Steve Naroff6f49f5d2007-05-29 14:23:36 +0000555 SourceLocation l = argExpr->getLocStart();
Steve Naroff17f76e02007-05-03 21:03:48 +0000556
557 // decode the result (notice that AST's are still created for extensions).
Steve Naroff17f76e02007-05-03 21:03:48 +0000558 switch (result) {
559 case Compatible:
560 break;
561 case PointerFromInt:
Steve Naroff218bc2b2007-05-04 21:54:46 +0000562 // check for null pointer constant (C99 6.3.2.3p3)
Chris Lattner0e9d6222007-07-15 23:26:56 +0000563 if (!argExpr->isNullPointerConstant(Context)) {
Steve Naroff56faab22007-05-30 04:20:12 +0000564 Diag(l, diag::ext_typecheck_passing_pointer_int,
565 lhsType.getAsString(), rhsType.getAsString(),
Chris Lattner38dbdb22007-07-21 03:03:59 +0000566 Fn->getSourceRange(), argExpr->getSourceRange());
Steve Naroffeb9da942007-05-30 00:06:37 +0000567 }
Steve Naroff17f76e02007-05-03 21:03:48 +0000568 break;
569 case IntFromPointer:
Steve Naroff56faab22007-05-30 04:20:12 +0000570 Diag(l, diag::ext_typecheck_passing_pointer_int,
571 lhsType.getAsString(), rhsType.getAsString(),
Chris Lattner38dbdb22007-07-21 03:03:59 +0000572 Fn->getSourceRange(), argExpr->getSourceRange());
Steve Naroff17f76e02007-05-03 21:03:48 +0000573 break;
574 case IncompatiblePointer:
Steve Naroff8563f652007-05-28 19:25:56 +0000575 Diag(l, diag::ext_typecheck_passing_incompatible_pointer,
Steve Naroffeb9da942007-05-30 00:06:37 +0000576 rhsType.getAsString(), lhsType.getAsString(),
Chris Lattner38dbdb22007-07-21 03:03:59 +0000577 Fn->getSourceRange(), argExpr->getSourceRange());
Steve Naroff17f76e02007-05-03 21:03:48 +0000578 break;
Steve Naroff1f4d7272007-05-11 04:00:31 +0000579 case CompatiblePointerDiscardsQualifiers:
Steve Naroffeb9da942007-05-30 00:06:37 +0000580 Diag(l, diag::ext_typecheck_passing_discards_qualifiers,
581 rhsType.getAsString(), lhsType.getAsString(),
Chris Lattner38dbdb22007-07-21 03:03:59 +0000582 Fn->getSourceRange(), argExpr->getSourceRange());
Steve Naroff1f4d7272007-05-11 04:00:31 +0000583 break;
Steve Naroff17f76e02007-05-03 21:03:48 +0000584 case Incompatible:
Steve Naroff8563f652007-05-28 19:25:56 +0000585 return Diag(l, diag::err_typecheck_passing_incompatible,
586 rhsType.getAsString(), lhsType.getAsString(),
Chris Lattner38dbdb22007-07-21 03:03:59 +0000587 Fn->getSourceRange(), argExpr->getSourceRange());
Steve Naroff17f76e02007-05-03 21:03:48 +0000588 }
589 }
Steve Naroffb8c289d2007-05-08 22:18:00 +0000590 // Even if the types checked, bail if we had the wrong number of arguments.
Chris Lattner38dbdb22007-07-21 03:03:59 +0000591 if (NumArgsInCall != NumArgsInProto && !proto->isVariadic())
Steve Naroffb8c289d2007-05-08 22:18:00 +0000592 return true;
Steve Naroffae4143e2007-04-26 20:39:23 +0000593 }
Chris Lattnerb87b1b32007-08-10 20:18:51 +0000594
595 // Do special checking on direct calls to functions.
596 if (ImplicitCastExpr *IcExpr = dyn_cast<ImplicitCastExpr>(Fn))
597 if (DeclRefExpr *DRExpr = dyn_cast<DeclRefExpr>(IcExpr->getSubExpr()))
598 if (FunctionDecl *FDecl = dyn_cast<FunctionDecl>(DRExpr->getDecl()))
Anders Carlssona3a9c432007-08-17 15:44:17 +0000599 if (CheckFunctionCall(Fn, LParenLoc, RParenLoc, FDecl, Args, NumArgsInCall))
Anders Carlsson98f07902007-08-17 05:31:46 +0000600 return true;
Chris Lattnerb87b1b32007-08-10 20:18:51 +0000601
Chris Lattner38dbdb22007-07-21 03:03:59 +0000602 return new CallExpr(Fn, Args, NumArgsInCall, resultType, RParenLoc);
Chris Lattnere168f762006-11-10 05:29:30 +0000603}
604
605Action::ExprResult Sema::
Steve Narofffbd09832007-07-19 01:06:55 +0000606ParseCompoundLiteral(SourceLocation LParenLoc, TypeTy *Ty,
Steve Naroff57eb2c52007-07-19 21:32:11 +0000607 SourceLocation RParenLoc, ExprTy *InitExpr) {
Steve Narofffbd09832007-07-19 01:06:55 +0000608 assert((Ty != 0) && "ParseCompoundLiteral(): missing type");
609 QualType literalType = QualType::getFromOpaquePtr(Ty);
Steve Naroff57eb2c52007-07-19 21:32:11 +0000610 // FIXME: put back this assert when initializers are worked out.
611 //assert((InitExpr != 0) && "ParseCompoundLiteral(): missing expression");
612 Expr *literalExpr = static_cast<Expr*>(InitExpr);
Steve Narofffbd09832007-07-19 01:06:55 +0000613
614 // FIXME: add semantic analysis (C99 6.5.2.5).
Steve Naroff57eb2c52007-07-19 21:32:11 +0000615 return new CompoundLiteralExpr(literalType, literalExpr);
Steve Narofffbd09832007-07-19 01:06:55 +0000616}
617
618Action::ExprResult Sema::
619ParseInitList(SourceLocation LParenLoc, ExprTy **InitList, unsigned NumInit,
620 SourceLocation RParenLoc) {
621 // FIXME: add semantic analysis (C99 6.7.8). This involves
622 // knowledge of the object being intialized. As a result, the code for
623 // doing the semantic analysis will likely be located elsewhere (i.e. in
624 // consumers of InitListExpr (e.g. ParseDeclarator, ParseCompoundLiteral).
625 return false; // FIXME instantiate an InitListExpr.
626}
627
628Action::ExprResult Sema::
Chris Lattnere168f762006-11-10 05:29:30 +0000629ParseCastExpr(SourceLocation LParenLoc, TypeTy *Ty,
630 SourceLocation RParenLoc, ExprTy *Op) {
Steve Naroff1a2cf6b2007-07-16 23:25:18 +0000631 assert((Ty != 0) && (Op != 0) && "ParseCastExpr(): missing type or expr");
632
633 Expr *castExpr = static_cast<Expr*>(Op);
634 QualType castType = QualType::getFromOpaquePtr(Ty);
635
Chris Lattnerbd270732007-07-18 16:00:06 +0000636 // C99 6.5.4p2: the cast type needs to be void or scalar and the expression
637 // type needs to be scalar.
638 if (!castType->isScalarType() && !castType->isVoidType()) {
Steve Naroff1a2cf6b2007-07-16 23:25:18 +0000639 return Diag(LParenLoc, diag::err_typecheck_cond_expect_scalar,
640 castType.getAsString(), SourceRange(LParenLoc, RParenLoc));
641 }
642 if (!castExpr->getType()->isScalarType()) {
643 return Diag(castExpr->getLocStart(),
644 diag::err_typecheck_expect_scalar_operand,
645 castExpr->getType().getAsString(), castExpr->getSourceRange());
646 }
647 return new CastExpr(castType, castExpr, LParenLoc);
Chris Lattnere168f762006-11-10 05:29:30 +0000648}
649
Steve Narofff8a28c52007-05-15 20:29:32 +0000650inline QualType Sema::CheckConditionalOperands( // C99 6.5.15
Steve Naroff7a5af782007-07-13 16:58:59 +0000651 Expr *&cond, Expr *&lex, Expr *&rex, SourceLocation questionLoc) {
Steve Naroff31090012007-07-16 21:54:35 +0000652 UsualUnaryConversions(cond);
653 UsualUnaryConversions(lex);
654 UsualUnaryConversions(rex);
655 QualType condT = cond->getType();
656 QualType lexT = lex->getType();
657 QualType rexT = rex->getType();
658
Steve Naroffa78fe7e2007-05-16 19:47:19 +0000659 // first, check the condition.
Steve Naroff7a5af782007-07-13 16:58:59 +0000660 if (!condT->isScalarType()) { // C99 6.5.15p2
661 Diag(cond->getLocStart(), diag::err_typecheck_cond_expect_scalar,
662 condT.getAsString());
Steve Naroffa78fe7e2007-05-16 19:47:19 +0000663 return QualType();
664 }
665 // now check the two expressions.
Steve Naroffdbd9e892007-07-17 00:58:39 +0000666 if (lexT->isArithmeticType() && rexT->isArithmeticType()) { // C99 6.5.15p3,5
667 UsualArithmeticConversions(lex, rex);
668 return lex->getType();
669 }
Chris Lattnere5a6cbd2007-07-31 21:27:01 +0000670 if (const RecordType *LHSRT = lexT->getAsRecordType()) { // C99 6.5.15p3
671 if (const RecordType *RHSRT = rexT->getAsRecordType()) {
672
673 if (LHSRT->getDecl()->getIdentifier() ==RHSRT->getDecl()->getIdentifier())
674 return lexT;
675
Steve Naroffa78fe7e2007-05-16 19:47:19 +0000676 Diag(questionLoc, diag::err_typecheck_cond_incompatible_operands,
Steve Naroff7a5af782007-07-13 16:58:59 +0000677 lexT.getAsString(), rexT.getAsString(),
678 lex->getSourceRange(), rex->getSourceRange());
Steve Naroffa78fe7e2007-05-16 19:47:19 +0000679 return QualType();
680 }
681 }
Chris Lattner0e9d6222007-07-15 23:26:56 +0000682 // C99 6.5.15p3
683 if (lexT->isPointerType() && rex->isNullPointerConstant(Context))
Steve Naroff7a5af782007-07-13 16:58:59 +0000684 return lexT;
Chris Lattner0e9d6222007-07-15 23:26:56 +0000685 if (rexT->isPointerType() && lex->isNullPointerConstant(Context))
Steve Naroff7a5af782007-07-13 16:58:59 +0000686 return rexT;
Steve Naroff30d1fbc2007-05-20 19:46:53 +0000687
Chris Lattnere5a6cbd2007-07-31 21:27:01 +0000688 if (const PointerType *LHSPT = lexT->getAsPointerType()) { // C99 6.5.15p3,6
689 if (const PointerType *RHSPT = rexT->getAsPointerType()) {
690 // get the "pointed to" types
691 QualType lhptee = LHSPT->getPointeeType();
692 QualType rhptee = RHSPT->getPointeeType();
Steve Naroffa78fe7e2007-05-16 19:47:19 +0000693
Chris Lattnere5a6cbd2007-07-31 21:27:01 +0000694 // ignore qualifiers on void (C99 6.5.15p3, clause 6)
695 if (lhptee->isVoidType() &&
696 (rhptee->isObjectType() || rhptee->isIncompleteType()))
697 return lexT;
698 if (rhptee->isVoidType() &&
699 (lhptee->isObjectType() || lhptee->isIncompleteType()))
700 return rexT;
Steve Naroffa78fe7e2007-05-16 19:47:19 +0000701
Chris Lattnere5a6cbd2007-07-31 21:27:01 +0000702 if (!Type::typesAreCompatible(lhptee.getUnqualifiedType(),
703 rhptee.getUnqualifiedType())) {
704 Diag(questionLoc, diag::ext_typecheck_cond_incompatible_pointers,
705 lexT.getAsString(), rexT.getAsString(),
706 lex->getSourceRange(), rex->getSourceRange());
707 return lexT; // FIXME: this is an _ext - is this return o.k?
708 }
709 // The pointer types are compatible.
710 // C99 6.5.15p6: If both operands are pointers to compatible types *or* to
711 // differently qualified versions of compatible types, the result type is a
712 // pointer to an appropriately qualified version of the *composite* type.
713 return lexT; // FIXME: Need to return the composite type.
Steve Naroffa78fe7e2007-05-16 19:47:19 +0000714 }
715 }
Chris Lattnere5a6cbd2007-07-31 21:27:01 +0000716
Steve Naroff7a5af782007-07-13 16:58:59 +0000717 if (lexT->isVoidType() && rexT->isVoidType()) // C99 6.5.15p3
718 return lexT;
Steve Naroffa78fe7e2007-05-16 19:47:19 +0000719
720 Diag(questionLoc, diag::err_typecheck_cond_incompatible_operands,
Steve Naroff7a5af782007-07-13 16:58:59 +0000721 lexT.getAsString(), rexT.getAsString(),
722 lex->getSourceRange(), rex->getSourceRange());
Steve Naroffa78fe7e2007-05-16 19:47:19 +0000723 return QualType();
Steve Narofff8a28c52007-05-15 20:29:32 +0000724}
725
Chris Lattnere168f762006-11-10 05:29:30 +0000726/// ParseConditionalOp - Parse a ?: operation. Note that 'LHS' may be null
727/// in the case of a the GNU conditional expr extension.
728Action::ExprResult Sema::ParseConditionalOp(SourceLocation QuestionLoc,
729 SourceLocation ColonLoc,
730 ExprTy *Cond, ExprTy *LHS,
731 ExprTy *RHS) {
Chris Lattnerdaaa9f22007-07-16 21:39:03 +0000732 Expr *CondExpr = (Expr *) Cond;
733 Expr *LHSExpr = (Expr *) LHS, *RHSExpr = (Expr *) RHS;
734 QualType result = CheckConditionalOperands(CondExpr, LHSExpr,
735 RHSExpr, QuestionLoc);
Steve Narofff8a28c52007-05-15 20:29:32 +0000736 if (result.isNull())
737 return true;
Chris Lattnerdaaa9f22007-07-16 21:39:03 +0000738 return new ConditionalOperator(CondExpr, LHSExpr, RHSExpr, result);
Chris Lattnere168f762006-11-10 05:29:30 +0000739}
740
Steve Naroff81569d22007-07-15 02:02:06 +0000741// promoteExprToType - a helper function to ensure we create exactly one
742// ImplicitCastExpr. As a convenience (to the caller), we return the type.
Steve Naroffdbd9e892007-07-17 00:58:39 +0000743static void promoteExprToType(Expr *&expr, QualType type) {
Steve Naroff81569d22007-07-15 02:02:06 +0000744 if (ImplicitCastExpr *impCast = dyn_cast<ImplicitCastExpr>(expr))
745 impCast->setType(type);
746 else
747 expr = new ImplicitCastExpr(type, expr);
Steve Naroffdbd9e892007-07-17 00:58:39 +0000748 return;
Steve Naroff81569d22007-07-15 02:02:06 +0000749}
750
751/// DefaultFunctionArrayConversion (C99 6.3.2.1p3, C99 6.3.2.1p4).
Steve Naroff31090012007-07-16 21:54:35 +0000752void Sema::DefaultFunctionArrayConversion(Expr *&e) {
Steve Naroff81569d22007-07-15 02:02:06 +0000753 QualType t = e->getType();
Steve Naroffb8ea4fb2007-07-13 23:32:42 +0000754 assert(!t.isNull() && "DefaultFunctionArrayConversion - missing type");
Bill Wendlingdfc81072007-07-17 03:52:31 +0000755
Chris Lattnercd1d0862007-07-31 16:56:34 +0000756 if (const ReferenceType *ref = t->getAsReferenceType()) {
Bill Wendling354fb262007-07-17 04:16:47 +0000757 promoteExprToType(e, ref->getReferenceeType()); // C++ [expr]
758 t = e->getType();
759 }
Steve Naroff81569d22007-07-15 02:02:06 +0000760 if (t->isFunctionType())
Steve Naroff31090012007-07-16 21:54:35 +0000761 promoteExprToType(e, Context.getPointerType(t));
Chris Lattner41977962007-07-31 19:29:30 +0000762 else if (const ArrayType *ary = t->getAsArrayType())
Steve Naroff31090012007-07-16 21:54:35 +0000763 promoteExprToType(e, Context.getPointerType(ary->getElementType()));
Steve Naroff1926c832007-04-24 00:23:05 +0000764}
765
Steve Naroff71b59a92007-06-04 22:22:31 +0000766/// UsualUnaryConversion - Performs various conversions that are common to most
767/// operators (C99 6.3). The conversions of array and function types are
768/// sometimes surpressed. For example, the array->pointer conversion doesn't
769/// apply if the array is an argument to the sizeof or address (&) operators.
770/// In these instances, this routine should *not* be called.
Steve Naroff31090012007-07-16 21:54:35 +0000771void Sema::UsualUnaryConversions(Expr *&expr) {
Steve Naroff7a5af782007-07-13 16:58:59 +0000772 QualType t = expr->getType();
Steve Naroff71b59a92007-06-04 22:22:31 +0000773 assert(!t.isNull() && "UsualUnaryConversions - missing type");
774
Chris Lattnercd1d0862007-07-31 16:56:34 +0000775 if (const ReferenceType *ref = t->getAsReferenceType()) {
Bill Wendling354fb262007-07-17 04:16:47 +0000776 promoteExprToType(expr, ref->getReferenceeType()); // C++ [expr]
777 t = expr->getType();
778 }
Steve Naroff81569d22007-07-15 02:02:06 +0000779 if (t->isPromotableIntegerType()) // C99 6.3.1.1p2
Steve Naroff31090012007-07-16 21:54:35 +0000780 promoteExprToType(expr, Context.IntTy);
781 else
782 DefaultFunctionArrayConversion(expr);
Steve Naroff71b59a92007-06-04 22:22:31 +0000783}
784
Steve Naroff1926c832007-04-24 00:23:05 +0000785/// UsualArithmeticConversions - Performs various conversions that are common to
786/// binary operators (C99 6.3.1.8). If both operands aren't arithmetic, this
787/// routine returns the first non-arithmetic type found. The client is
788/// responsible for emitting appropriate error diagnostics.
Steve Naroffbe4c4d12007-08-24 19:07:16 +0000789QualType Sema::UsualArithmeticConversions(Expr *&lhsExpr, Expr *&rhsExpr,
790 bool isCompAssign) {
Steve Naroff46c72912007-08-25 19:54:59 +0000791 if (!isCompAssign) {
792 UsualUnaryConversions(lhsExpr);
793 UsualUnaryConversions(rhsExpr);
794 }
Steve Naroff94a5aca2007-07-16 22:23:01 +0000795 QualType lhs = lhsExpr->getType();
796 QualType rhs = rhsExpr->getType();
Steve Naroff1926c832007-04-24 00:23:05 +0000797
Chris Lattner0c46c5d2007-06-02 23:53:17 +0000798 // If both types are identical, no conversion is needed.
799 if (lhs == rhs)
Steve Naroffbe4c4d12007-08-24 19:07:16 +0000800 return lhs;
Chris Lattner0c46c5d2007-06-02 23:53:17 +0000801
802 // If either side is a non-arithmetic type (e.g. a pointer), we are done.
803 // The caller can deal with this (e.g. pointer + int).
Steve Naroffdbd9e892007-07-17 00:58:39 +0000804 if (!lhs->isArithmeticType() || !rhs->isArithmeticType())
Steve Naroffbe4c4d12007-08-24 19:07:16 +0000805 return lhs;
Steve Naroff1926c832007-04-24 00:23:05 +0000806
Chris Lattner0c46c5d2007-06-02 23:53:17 +0000807 // At this point, we have two different arithmetic types.
Steve Naroffb01bbe32007-04-25 01:22:31 +0000808
809 // Handle complex types first (C99 6.3.1.8p1).
810 if (lhs->isComplexType() || rhs->isComplexType()) {
811 // if we have an integer operand, the result is the complex type.
Steve Naroffdbd9e892007-07-17 00:58:39 +0000812 if (rhs->isIntegerType()) { // convert the rhs to the lhs complex type.
Steve Naroffbe4c4d12007-08-24 19:07:16 +0000813 if (!isCompAssign) promoteExprToType(rhsExpr, lhs);
814 return lhs;
Steve Naroffdbd9e892007-07-17 00:58:39 +0000815 }
816 if (lhs->isIntegerType()) { // convert the lhs to the rhs complex type.
Steve Naroffbe4c4d12007-08-24 19:07:16 +0000817 if (!isCompAssign) promoteExprToType(lhsExpr, rhs);
818 return rhs;
Steve Naroffdbd9e892007-07-17 00:58:39 +0000819 }
Steve Naroff9091ef72007-08-27 01:27:54 +0000820 // This handles complex/complex, complex/float, or float/complex.
821 // When both operands are complex, the shorter operand is converted to the
822 // type of the longer, and that is the type of the result. This corresponds
823 // to what is done when combining two real floating-point operands.
824 // The fun begins when size promotion occur across type domains.
825 // From H&S 6.3.4: When one operand is complex and the other is a real
826 // floating-point type, the less precise type is converted, within it's
827 // real or complex domain, to the precision of the other type. For example,
828 // when combining a "long double" with a "double _Complex", the
829 // "double _Complex" is promoted to "long double _Complex".
Steve Naroff7af82d42007-08-27 15:30:22 +0000830 int result = Context.compareFloatingType(lhs, rhs);
831
832 if (result > 0) { // The left side is bigger, convert rhs.
Steve Naroffe31313d2007-08-27 21:32:55 +0000833 rhs = Context.getFloatingTypeOfSizeWithinDomain(lhs, rhs);
834 if (!isCompAssign)
835 promoteExprToType(rhsExpr, rhs);
836 } else if (result < 0) { // The right side is bigger, convert lhs.
837 lhs = Context.getFloatingTypeOfSizeWithinDomain(rhs, lhs);
838 if (!isCompAssign)
839 promoteExprToType(lhsExpr, lhs);
840 }
841 // At this point, lhs and rhs have the same rank/size. Now, make sure the
842 // domains match. This is a requirement for our implementation, C99
843 // does not require this promotion.
844 if (lhs != rhs) { // Domains don't match, we have complex/float mix.
845 if (lhs->isRealFloatingType()) { // handle "double, _Complex double".
Steve Naroffa042db22007-08-27 21:43:43 +0000846 if (!isCompAssign)
847 promoteExprToType(lhsExpr, rhs);
848 return rhs;
Steve Naroffe31313d2007-08-27 21:32:55 +0000849 } else { // handle "_Complex double, double".
Steve Naroffa042db22007-08-27 21:43:43 +0000850 if (!isCompAssign)
851 promoteExprToType(rhsExpr, lhs);
852 return lhs;
Steve Naroffe31313d2007-08-27 21:32:55 +0000853 }
Steve Naroffdbd9e892007-07-17 00:58:39 +0000854 }
Steve Naroffa042db22007-08-27 21:43:43 +0000855 return lhs; // The domain/size match exactly.
Steve Naroffbf223ba2007-04-24 20:56:26 +0000856 }
Steve Naroffb01bbe32007-04-25 01:22:31 +0000857 // Now handle "real" floating types (i.e. float, double, long double).
858 if (lhs->isRealFloatingType() || rhs->isRealFloatingType()) {
859 // if we have an integer operand, the result is the real floating type.
Steve Naroffdbd9e892007-07-17 00:58:39 +0000860 if (rhs->isIntegerType()) { // convert rhs to the lhs floating point type.
Steve Naroffbe4c4d12007-08-24 19:07:16 +0000861 if (!isCompAssign) promoteExprToType(rhsExpr, lhs);
862 return lhs;
Steve Naroffdbd9e892007-07-17 00:58:39 +0000863 }
864 if (lhs->isIntegerType()) { // convert lhs to the rhs floating point type.
Steve Naroffbe4c4d12007-08-24 19:07:16 +0000865 if (!isCompAssign) promoteExprToType(lhsExpr, rhs);
866 return rhs;
Steve Naroffdbd9e892007-07-17 00:58:39 +0000867 }
Steve Naroff81569d22007-07-15 02:02:06 +0000868 // We have two real floating types, float/complex combos were handled above.
869 // Convert the smaller operand to the bigger result.
Steve Naroff7af82d42007-08-27 15:30:22 +0000870 int result = Context.compareFloatingType(lhs, rhs);
871
872 if (result > 0) { // convert the rhs
Steve Naroffbe4c4d12007-08-24 19:07:16 +0000873 if (!isCompAssign) promoteExprToType(rhsExpr, lhs);
874 return lhs;
Steve Naroffdbd9e892007-07-17 00:58:39 +0000875 }
Steve Naroff7af82d42007-08-27 15:30:22 +0000876 if (result < 0) { // convert the lhs
877 if (!isCompAssign) promoteExprToType(lhsExpr, rhs); // convert the lhs
878 return rhs;
879 }
880 assert(0 && "Sema::UsualArithmeticConversions(): illegal float comparison");
Steve Naroffb01bbe32007-04-25 01:22:31 +0000881 }
Steve Naroff81569d22007-07-15 02:02:06 +0000882 // Finally, we have two differing integer types.
Steve Naroffdbd9e892007-07-17 00:58:39 +0000883 if (Context.maxIntegerType(lhs, rhs) == lhs) { // convert the rhs
Steve Naroffbe4c4d12007-08-24 19:07:16 +0000884 if (!isCompAssign) promoteExprToType(rhsExpr, lhs);
885 return lhs;
Steve Naroffdbd9e892007-07-17 00:58:39 +0000886 }
Steve Naroffbe4c4d12007-08-24 19:07:16 +0000887 if (!isCompAssign) promoteExprToType(lhsExpr, rhs); // convert the lhs
888 return rhs;
Steve Narofff1e53692007-03-23 22:27:02 +0000889}
890
Steve Naroff3f597292007-05-11 22:18:03 +0000891// CheckPointerTypesForAssignment - This is a very tricky routine (despite
892// being closely modeled after the C99 spec:-). The odd characteristic of this
893// routine is it effectively iqnores the qualifiers on the top level pointee.
894// This circumvents the usual type rules specified in 6.2.7p1 & 6.7.5.[1-3].
895// FIXME: add a couple examples in this comment.
Steve Naroff98cf3e92007-06-06 18:38:38 +0000896Sema::AssignmentCheckResult
897Sema::CheckPointerTypesForAssignment(QualType lhsType, QualType rhsType) {
Steve Naroff3f597292007-05-11 22:18:03 +0000898 QualType lhptee, rhptee;
Steve Naroff1f4d7272007-05-11 04:00:31 +0000899
900 // get the "pointed to" type (ignoring qualifiers at the top level)
Chris Lattnere5a6cbd2007-07-31 21:27:01 +0000901 lhptee = lhsType->getAsPointerType()->getPointeeType();
902 rhptee = rhsType->getAsPointerType()->getPointeeType();
Steve Naroff1f4d7272007-05-11 04:00:31 +0000903
904 // make sure we operate on the canonical type
Steve Naroff3f597292007-05-11 22:18:03 +0000905 lhptee = lhptee.getCanonicalType();
906 rhptee = rhptee.getCanonicalType();
Steve Naroff1f4d7272007-05-11 04:00:31 +0000907
Steve Naroff98cf3e92007-06-06 18:38:38 +0000908 AssignmentCheckResult r = Compatible;
909
Steve Naroff3f597292007-05-11 22:18:03 +0000910 // C99 6.5.16.1p1: This following citation is common to constraints
911 // 3 & 4 (below). ...and the type *pointed to* by the left has all the
912 // qualifiers of the type *pointed to* by the right;
913 if ((lhptee.getQualifiers() & rhptee.getQualifiers()) !=
914 rhptee.getQualifiers())
915 r = CompatiblePointerDiscardsQualifiers;
916
917 // C99 6.5.16.1p1 (constraint 4): If one operand is a pointer to an object or
918 // incomplete type and the other is a pointer to a qualified or unqualified
919 // version of void...
920 if (lhptee.getUnqualifiedType()->isVoidType() &&
921 (rhptee->isObjectType() || rhptee->isIncompleteType()))
922 ;
923 else if (rhptee.getUnqualifiedType()->isVoidType() &&
924 (lhptee->isObjectType() || lhptee->isIncompleteType()))
925 ;
926 // C99 6.5.16.1p1 (constraint 3): both operands are pointers to qualified or
927 // unqualified versions of compatible types, ...
928 else if (!Type::typesAreCompatible(lhptee.getUnqualifiedType(),
929 rhptee.getUnqualifiedType()))
930 r = IncompatiblePointer; // this "trumps" PointerAssignDiscardsQualifiers
Steve Naroff98cf3e92007-06-06 18:38:38 +0000931 return r;
Steve Naroff1f4d7272007-05-11 04:00:31 +0000932}
933
Steve Naroff98cf3e92007-06-06 18:38:38 +0000934/// CheckAssignmentConstraints (C99 6.5.16) - This routine currently
Steve Naroff17f76e02007-05-03 21:03:48 +0000935/// has code to accommodate several GCC extensions when type checking
936/// pointers. Here are some objectionable examples that GCC considers warnings:
937///
938/// int a, *pint;
939/// short *pshort;
940/// struct foo *pfoo;
941///
942/// pint = pshort; // warning: assignment from incompatible pointer type
943/// a = pint; // warning: assignment makes integer from pointer without a cast
944/// pint = a; // warning: assignment makes pointer from integer without a cast
945/// pint = pfoo; // warning: assignment from incompatible pointer type
946///
947/// As a result, the code for dealing with pointers is more complex than the
948/// C99 spec dictates.
949/// Note: the warning above turn into errors when -pedantic-errors is enabled.
950///
Steve Naroff98cf3e92007-06-06 18:38:38 +0000951Sema::AssignmentCheckResult
952Sema::CheckAssignmentConstraints(QualType lhsType, QualType rhsType) {
Steve Naroff44fd8ff2007-07-24 21:46:40 +0000953 if (lhsType == rhsType) // common case, fast path...
954 return Compatible;
955
Steve Naroff84ff4b42007-07-09 21:31:10 +0000956 if (lhsType->isArithmeticType() && rhsType->isArithmeticType()) {
Steve Naroffe728ba32007-07-10 22:20:04 +0000957 if (lhsType->isVectorType() || rhsType->isVectorType()) {
958 if (lhsType.getCanonicalType() != rhsType.getCanonicalType())
959 return Incompatible;
960 }
Steve Naroff98cf3e92007-06-06 18:38:38 +0000961 return Compatible;
Steve Naroff84ff4b42007-07-09 21:31:10 +0000962 } else if (lhsType->isPointerType()) {
Steve Naroff98cf3e92007-06-06 18:38:38 +0000963 if (rhsType->isIntegerType())
964 return PointerFromInt;
965
Steve Naroff3f597292007-05-11 22:18:03 +0000966 if (rhsType->isPointerType())
Steve Naroff98cf3e92007-06-06 18:38:38 +0000967 return CheckPointerTypesForAssignment(lhsType, rhsType);
Steve Naroff9eb24652007-05-02 21:58:15 +0000968 } else if (rhsType->isPointerType()) {
Steve Naroff98cf3e92007-06-06 18:38:38 +0000969 // C99 6.5.16.1p1: the left operand is _Bool and the right is a pointer.
970 if ((lhsType->isIntegerType()) && (lhsType != Context.BoolTy))
971 return IntFromPointer;
972
Steve Naroff3f597292007-05-11 22:18:03 +0000973 if (lhsType->isPointerType())
Steve Naroff98cf3e92007-06-06 18:38:38 +0000974 return CheckPointerTypesForAssignment(lhsType, rhsType);
Steve Naroff1f4d7272007-05-11 04:00:31 +0000975 } else if (isa<TagType>(lhsType) && isa<TagType>(rhsType)) {
976 if (Type::tagTypesAreCompatible(lhsType, rhsType))
Steve Naroff98cf3e92007-06-06 18:38:38 +0000977 return Compatible;
Bill Wendlingdb4f06e2007-06-02 23:29:59 +0000978 } else if (lhsType->isReferenceType() || rhsType->isReferenceType()) {
979 if (Type::referenceTypesAreCompatible(lhsType, rhsType))
Steve Naroff98cf3e92007-06-06 18:38:38 +0000980 return Compatible;
Bill Wendling216423b2007-05-30 06:30:29 +0000981 }
Steve Naroff98cf3e92007-06-06 18:38:38 +0000982 return Incompatible;
Steve Naroff9eb24652007-05-02 21:58:15 +0000983}
984
Steve Naroffb8ea4fb2007-07-13 23:32:42 +0000985Sema::AssignmentCheckResult
986Sema::CheckSingleAssignmentConstraints(QualType lhsType, Expr *&rExpr) {
987 // This check seems unnatural, however it is necessary to insure the proper
988 // conversion of functions/arrays. If the conversion were done for all
989 // DeclExpr's (created by ParseIdentifierExpr), it would mess up the unary
990 // expressions that surpress this implicit conversion (&, sizeof).
Steve Naroff31090012007-07-16 21:54:35 +0000991 DefaultFunctionArrayConversion(rExpr);
Steve Naroff0c1c7ed2007-08-24 22:33:52 +0000992
993 Sema::AssignmentCheckResult result;
Steve Naroffb8ea4fb2007-07-13 23:32:42 +0000994
Steve Naroff0c1c7ed2007-08-24 22:33:52 +0000995 result = CheckAssignmentConstraints(lhsType, rExpr->getType());
996
997 // C99 6.5.16.1p2: The value of the right operand is converted to the
998 // type of the assignment expression.
999 if (rExpr->getType() != lhsType)
1000 promoteExprToType(rExpr, lhsType);
1001 return result;
Steve Naroffb8ea4fb2007-07-13 23:32:42 +00001002}
1003
1004Sema::AssignmentCheckResult
1005Sema::CheckCompoundAssignmentConstraints(QualType lhsType, QualType rhsType) {
1006 return CheckAssignmentConstraints(lhsType, rhsType);
1007}
1008
Steve Naroff7a5af782007-07-13 16:58:59 +00001009inline void Sema::InvalidOperands(SourceLocation loc, Expr *&lex, Expr *&rex) {
Steve Naroff6f49f5d2007-05-29 14:23:36 +00001010 Diag(loc, diag::err_typecheck_invalid_operands,
1011 lex->getType().getAsString(), rex->getType().getAsString(),
1012 lex->getSourceRange(), rex->getSourceRange());
1013}
1014
Steve Naroff7a5af782007-07-13 16:58:59 +00001015inline QualType Sema::CheckVectorOperands(SourceLocation loc, Expr *&lex,
1016 Expr *&rex) {
Steve Naroff84ff4b42007-07-09 21:31:10 +00001017 QualType lhsType = lex->getType(), rhsType = rex->getType();
1018
1019 // make sure the vector types are identical.
1020 if (lhsType == rhsType)
1021 return lhsType;
1022 // You cannot convert between vector values of different size.
1023 Diag(loc, diag::err_typecheck_vector_not_convertable,
1024 lex->getType().getAsString(), rex->getType().getAsString(),
1025 lex->getSourceRange(), rex->getSourceRange());
1026 return QualType();
1027}
1028
Steve Naroff218bc2b2007-05-04 21:54:46 +00001029inline QualType Sema::CheckMultiplyDivideOperands(
Steve Naroffbe4c4d12007-08-24 19:07:16 +00001030 Expr *&lex, Expr *&rex, SourceLocation loc, bool isCompAssign)
Steve Naroff5c10d4b2007-04-20 23:42:24 +00001031{
Steve Naroffb8ea4fb2007-07-13 23:32:42 +00001032 QualType lhsType = lex->getType(), rhsType = rex->getType();
1033
1034 if (lhsType->isVectorType() || rhsType->isVectorType())
Steve Naroff84ff4b42007-07-09 21:31:10 +00001035 return CheckVectorOperands(loc, lex, rex);
Steve Naroff7a5af782007-07-13 16:58:59 +00001036
Steve Naroffbe4c4d12007-08-24 19:07:16 +00001037 QualType compType = UsualArithmeticConversions(lex, rex, isCompAssign);
Steve Naroff5c10d4b2007-04-20 23:42:24 +00001038
Steve Naroffdbd9e892007-07-17 00:58:39 +00001039 if (lex->getType()->isArithmeticType() && rex->getType()->isArithmeticType())
Steve Naroffbe4c4d12007-08-24 19:07:16 +00001040 return compType;
Steve Naroff6f49f5d2007-05-29 14:23:36 +00001041 InvalidOperands(loc, lex, rex);
Steve Naroff218bc2b2007-05-04 21:54:46 +00001042 return QualType();
Steve Naroff26c8ea52007-03-21 21:08:52 +00001043}
1044
Steve Naroff218bc2b2007-05-04 21:54:46 +00001045inline QualType Sema::CheckRemainderOperands(
Steve Naroffbe4c4d12007-08-24 19:07:16 +00001046 Expr *&lex, Expr *&rex, SourceLocation loc, bool isCompAssign)
Steve Naroff218bc2b2007-05-04 21:54:46 +00001047{
Steve Naroffb8ea4fb2007-07-13 23:32:42 +00001048 QualType lhsType = lex->getType(), rhsType = rex->getType();
1049
Steve Naroffbe4c4d12007-08-24 19:07:16 +00001050 QualType compType = UsualArithmeticConversions(lex, rex, isCompAssign);
Steve Naroff218bc2b2007-05-04 21:54:46 +00001051
Steve Naroffdbd9e892007-07-17 00:58:39 +00001052 if (lex->getType()->isIntegerType() && rex->getType()->isIntegerType())
Steve Naroffbe4c4d12007-08-24 19:07:16 +00001053 return compType;
Steve Naroff6f49f5d2007-05-29 14:23:36 +00001054 InvalidOperands(loc, lex, rex);
Steve Naroff218bc2b2007-05-04 21:54:46 +00001055 return QualType();
1056}
1057
1058inline QualType Sema::CheckAdditionOperands( // C99 6.5.6
Steve Naroffbe4c4d12007-08-24 19:07:16 +00001059 Expr *&lex, Expr *&rex, SourceLocation loc, bool isCompAssign)
Steve Naroff1926c832007-04-24 00:23:05 +00001060{
Steve Naroff94a5aca2007-07-16 22:23:01 +00001061 if (lex->getType()->isVectorType() || rex->getType()->isVectorType())
Steve Naroff7a5af782007-07-13 16:58:59 +00001062 return CheckVectorOperands(loc, lex, rex);
1063
Steve Naroffbe4c4d12007-08-24 19:07:16 +00001064 QualType compType = UsualArithmeticConversions(lex, rex, isCompAssign);
Steve Naroff94a5aca2007-07-16 22:23:01 +00001065
Steve Naroffe4718892007-04-27 18:30:00 +00001066 // handle the common case first (both operands are arithmetic).
Steve Naroffdbd9e892007-07-17 00:58:39 +00001067 if (lex->getType()->isArithmeticType() && rex->getType()->isArithmeticType())
Steve Naroffbe4c4d12007-08-24 19:07:16 +00001068 return compType;
Steve Naroff218bc2b2007-05-04 21:54:46 +00001069
Steve Naroffdbd9e892007-07-17 00:58:39 +00001070 if (lex->getType()->isPointerType() && rex->getType()->isIntegerType())
1071 return lex->getType();
1072 if (lex->getType()->isIntegerType() && rex->getType()->isPointerType())
1073 return rex->getType();
Steve Naroff6f49f5d2007-05-29 14:23:36 +00001074 InvalidOperands(loc, lex, rex);
Steve Naroff218bc2b2007-05-04 21:54:46 +00001075 return QualType();
Steve Naroff26c8ea52007-03-21 21:08:52 +00001076}
1077
Steve Naroff218bc2b2007-05-04 21:54:46 +00001078inline QualType Sema::CheckSubtractionOperands( // C99 6.5.6
Steve Naroffbe4c4d12007-08-24 19:07:16 +00001079 Expr *&lex, Expr *&rex, SourceLocation loc, bool isCompAssign)
Steve Naroff218bc2b2007-05-04 21:54:46 +00001080{
Steve Naroff94a5aca2007-07-16 22:23:01 +00001081 if (lex->getType()->isVectorType() || rex->getType()->isVectorType())
Steve Naroff84ff4b42007-07-09 21:31:10 +00001082 return CheckVectorOperands(loc, lex, rex);
Steve Naroffb8ea4fb2007-07-13 23:32:42 +00001083
Steve Naroffbe4c4d12007-08-24 19:07:16 +00001084 QualType compType = UsualArithmeticConversions(lex, rex, isCompAssign);
Steve Naroff218bc2b2007-05-04 21:54:46 +00001085
1086 // handle the common case first (both operands are arithmetic).
Steve Naroffdbd9e892007-07-17 00:58:39 +00001087 if (lex->getType()->isArithmeticType() && rex->getType()->isArithmeticType())
Steve Naroffbe4c4d12007-08-24 19:07:16 +00001088 return compType;
Steve Naroff94a5aca2007-07-16 22:23:01 +00001089
1090 if (lex->getType()->isPointerType() && rex->getType()->isIntegerType())
Steve Naroffbe4c4d12007-08-24 19:07:16 +00001091 return compType;
Steve Naroff94a5aca2007-07-16 22:23:01 +00001092 if (lex->getType()->isPointerType() && rex->getType()->isPointerType())
Chris Lattnerd2b88ab2007-07-13 03:05:23 +00001093 return Context.getPointerDiffType();
Steve Naroff6f49f5d2007-05-29 14:23:36 +00001094 InvalidOperands(loc, lex, rex);
Steve Naroff218bc2b2007-05-04 21:54:46 +00001095 return QualType();
1096}
1097
1098inline QualType Sema::CheckShiftOperands( // C99 6.5.7
Steve Naroffbe4c4d12007-08-24 19:07:16 +00001099 Expr *&lex, Expr *&rex, SourceLocation loc, bool isCompAssign)
Steve Naroff1926c832007-04-24 00:23:05 +00001100{
Chris Lattner1d411a82007-06-05 20:52:21 +00001101 // FIXME: Shifts don't perform usual arithmetic conversions. This is wrong
1102 // for int << longlong -> the result type should be int, not long long.
Steve Naroffbe4c4d12007-08-24 19:07:16 +00001103 QualType compType = UsualArithmeticConversions(lex, rex, isCompAssign);
Steve Naroff1926c832007-04-24 00:23:05 +00001104
Steve Naroffdbd9e892007-07-17 00:58:39 +00001105 // handle the common case first (both operands are arithmetic).
1106 if (lex->getType()->isIntegerType() && rex->getType()->isIntegerType())
Steve Naroffbe4c4d12007-08-24 19:07:16 +00001107 return compType;
Steve Naroff6f49f5d2007-05-29 14:23:36 +00001108 InvalidOperands(loc, lex, rex);
Steve Naroff218bc2b2007-05-04 21:54:46 +00001109 return QualType();
Steve Naroff26c8ea52007-03-21 21:08:52 +00001110}
1111
Chris Lattnerb620c342007-08-26 01:18:55 +00001112inline QualType Sema::CheckCompareOperands( // C99 6.5.8
1113 Expr *&lex, Expr *&rex, SourceLocation loc, bool isRelational)
Steve Naroff1926c832007-04-24 00:23:05 +00001114{
Chris Lattnerb620c342007-08-26 01:18:55 +00001115 // C99 6.5.8p3 / C99 6.5.9p4
Steve Naroff47fea352007-08-10 18:26:40 +00001116 if (lex->getType()->isArithmeticType() && rex->getType()->isArithmeticType())
1117 UsualArithmeticConversions(lex, rex);
1118 else {
1119 UsualUnaryConversions(lex);
1120 UsualUnaryConversions(rex);
1121 }
Steve Naroff31090012007-07-16 21:54:35 +00001122 QualType lType = lex->getType();
1123 QualType rType = rex->getType();
Steve Naroff1926c832007-04-24 00:23:05 +00001124
Chris Lattnerb620c342007-08-26 01:18:55 +00001125 if (isRelational) {
1126 if (lType->isRealType() && rType->isRealType())
1127 return Context.IntTy;
1128 } else {
1129 if (lType->isArithmeticType() && rType->isArithmeticType())
1130 return Context.IntTy;
1131 }
Steve Naroffe4718892007-04-27 18:30:00 +00001132
Chris Lattner1895e582007-08-26 01:10:14 +00001133 bool LHSIsNull = lex->isNullPointerConstant(Context);
1134 bool RHSIsNull = rex->isNullPointerConstant(Context);
1135
Chris Lattnerb620c342007-08-26 01:18:55 +00001136 // All of the following pointer related warnings are GCC extensions, except
1137 // when handling null pointer constants. One day, we can consider making them
1138 // errors (when -pedantic-errors is enabled).
Steve Naroff808eb8f2007-08-27 04:08:11 +00001139 if (lType->isPointerType() && rType->isPointerType()) { // C99 6.5.8p2
Chris Lattner1895e582007-08-26 01:10:14 +00001140 if (!LHSIsNull && !RHSIsNull &&
Steve Naroff808eb8f2007-08-27 04:08:11 +00001141 !Type::pointerTypesAreCompatible(lType.getUnqualifiedType(),
1142 rType.getUnqualifiedType())) {
Steve Naroffcdee44c2007-08-16 21:48:38 +00001143 Diag(loc, diag::ext_typecheck_comparison_of_distinct_pointers,
1144 lType.getAsString(), rType.getAsString(),
1145 lex->getSourceRange(), rex->getSourceRange());
Steve Naroff75c17232007-06-13 21:41:08 +00001146 }
Chris Lattner1895e582007-08-26 01:10:14 +00001147 promoteExprToType(rex, lType); // promote the pointer to pointer
Steve Naroffcdee44c2007-08-16 21:48:38 +00001148 return Context.IntTy;
1149 }
1150 if (lType->isPointerType() && rType->isIntegerType()) {
Chris Lattner1895e582007-08-26 01:10:14 +00001151 if (!RHSIsNull)
Steve Naroffcdee44c2007-08-16 21:48:38 +00001152 Diag(loc, diag::ext_typecheck_comparison_of_pointer_integer,
1153 lType.getAsString(), rType.getAsString(),
1154 lex->getSourceRange(), rex->getSourceRange());
Chris Lattner1895e582007-08-26 01:10:14 +00001155 promoteExprToType(rex, lType); // promote the integer to pointer
Steve Naroffcdee44c2007-08-16 21:48:38 +00001156 return Context.IntTy;
1157 }
1158 if (lType->isIntegerType() && rType->isPointerType()) {
Chris Lattner1895e582007-08-26 01:10:14 +00001159 if (!LHSIsNull)
Steve Naroffcdee44c2007-08-16 21:48:38 +00001160 Diag(loc, diag::ext_typecheck_comparison_of_pointer_integer,
1161 lType.getAsString(), rType.getAsString(),
1162 lex->getSourceRange(), rex->getSourceRange());
Chris Lattner1895e582007-08-26 01:10:14 +00001163 promoteExprToType(lex, rType); // promote the integer to pointer
Steve Naroffcdee44c2007-08-16 21:48:38 +00001164 return Context.IntTy;
Steve Naroff043d45d2007-05-15 02:32:35 +00001165 }
Steve Naroff6f49f5d2007-05-29 14:23:36 +00001166 InvalidOperands(loc, lex, rex);
Steve Naroff218bc2b2007-05-04 21:54:46 +00001167 return QualType();
Steve Naroff26c8ea52007-03-21 21:08:52 +00001168}
1169
Steve Naroff218bc2b2007-05-04 21:54:46 +00001170inline QualType Sema::CheckBitwiseOperands(
Steve Naroffbe4c4d12007-08-24 19:07:16 +00001171 Expr *&lex, Expr *&rex, SourceLocation loc, bool isCompAssign)
Steve Naroff1926c832007-04-24 00:23:05 +00001172{
Steve Naroff94a5aca2007-07-16 22:23:01 +00001173 if (lex->getType()->isVectorType() || rex->getType()->isVectorType())
Steve Naroff84ff4b42007-07-09 21:31:10 +00001174 return CheckVectorOperands(loc, lex, rex);
Steve Naroffb8ea4fb2007-07-13 23:32:42 +00001175
Steve Naroffbe4c4d12007-08-24 19:07:16 +00001176 QualType compType = UsualArithmeticConversions(lex, rex, isCompAssign);
Steve Naroff1926c832007-04-24 00:23:05 +00001177
Steve Naroffdbd9e892007-07-17 00:58:39 +00001178 if (lex->getType()->isIntegerType() && rex->getType()->isIntegerType())
Steve Naroffbe4c4d12007-08-24 19:07:16 +00001179 return compType;
Steve Naroff6f49f5d2007-05-29 14:23:36 +00001180 InvalidOperands(loc, lex, rex);
Steve Naroff218bc2b2007-05-04 21:54:46 +00001181 return QualType();
Steve Naroff26c8ea52007-03-21 21:08:52 +00001182}
1183
Steve Naroff218bc2b2007-05-04 21:54:46 +00001184inline QualType Sema::CheckLogicalOperands( // C99 6.5.[13,14]
Steve Naroff7a5af782007-07-13 16:58:59 +00001185 Expr *&lex, Expr *&rex, SourceLocation loc)
Steve Naroff1926c832007-04-24 00:23:05 +00001186{
Steve Naroff31090012007-07-16 21:54:35 +00001187 UsualUnaryConversions(lex);
1188 UsualUnaryConversions(rex);
Steve Naroffe4718892007-04-27 18:30:00 +00001189
Steve Naroffdbd9e892007-07-17 00:58:39 +00001190 if (lex->getType()->isScalarType() || rex->getType()->isScalarType())
Steve Naroff218bc2b2007-05-04 21:54:46 +00001191 return Context.IntTy;
Steve Naroff6f49f5d2007-05-29 14:23:36 +00001192 InvalidOperands(loc, lex, rex);
Steve Naroff218bc2b2007-05-04 21:54:46 +00001193 return QualType();
Steve Naroffae4143e2007-04-26 20:39:23 +00001194}
1195
Steve Naroff35d85152007-05-07 00:24:15 +00001196inline QualType Sema::CheckAssignmentOperands( // C99 6.5.16.1
Steve Naroff0c1c7ed2007-08-24 22:33:52 +00001197 Expr *lex, Expr *&rex, SourceLocation loc, QualType compoundType)
Steve Naroffae4143e2007-04-26 20:39:23 +00001198{
Steve Naroff0af91202007-04-27 21:51:21 +00001199 QualType lhsType = lex->getType();
Steve Naroff35d85152007-05-07 00:24:15 +00001200 QualType rhsType = compoundType.isNull() ? rex->getType() : compoundType;
Steve Naroff1f4d7272007-05-11 04:00:31 +00001201 bool hadError = false;
Steve Naroff9358c712007-05-27 23:58:33 +00001202 Expr::isModifiableLvalueResult mlval = lex->isModifiableLvalue();
1203
1204 switch (mlval) { // C99 6.5.16p2
1205 case Expr::MLV_Valid:
1206 break;
1207 case Expr::MLV_ConstQualified:
1208 Diag(loc, diag::err_typecheck_assign_const, lex->getSourceRange());
1209 hadError = true;
1210 break;
1211 case Expr::MLV_ArrayType:
1212 Diag(loc, diag::err_typecheck_array_not_modifiable_lvalue,
1213 lhsType.getAsString(), lex->getSourceRange());
1214 return QualType();
1215 case Expr::MLV_NotObjectType:
1216 Diag(loc, diag::err_typecheck_non_object_not_modifiable_lvalue,
1217 lhsType.getAsString(), lex->getSourceRange());
1218 return QualType();
1219 case Expr::MLV_InvalidExpression:
1220 Diag(loc, diag::err_typecheck_expression_not_modifiable_lvalue,
1221 lex->getSourceRange());
1222 return QualType();
1223 case Expr::MLV_IncompleteType:
1224 case Expr::MLV_IncompleteVoidType:
1225 Diag(loc, diag::err_typecheck_incomplete_type_not_modifiable_lvalue,
1226 lhsType.getAsString(), lex->getSourceRange());
1227 return QualType();
Steve Naroff0d595ca2007-07-30 03:29:09 +00001228 case Expr::MLV_DuplicateVectorComponents:
1229 Diag(loc, diag::err_typecheck_duplicate_vector_components_not_mlvalue,
1230 lex->getSourceRange());
1231 return QualType();
Steve Naroff218bc2b2007-05-04 21:54:46 +00001232 }
Steve Naroffb8ea4fb2007-07-13 23:32:42 +00001233 AssignmentCheckResult result;
1234
1235 if (compoundType.isNull())
1236 result = CheckSingleAssignmentConstraints(lhsType, rex);
1237 else
1238 result = CheckCompoundAssignmentConstraints(lhsType, rhsType);
Steve Naroffad373bd2007-07-31 12:34:36 +00001239
Steve Naroff218bc2b2007-05-04 21:54:46 +00001240 // decode the result (notice that extensions still return a type).
1241 switch (result) {
1242 case Compatible:
Steve Naroff1f4d7272007-05-11 04:00:31 +00001243 break;
Steve Naroff218bc2b2007-05-04 21:54:46 +00001244 case Incompatible:
Steve Naroffe845e272007-05-18 01:06:45 +00001245 Diag(loc, diag::err_typecheck_assign_incompatible,
Chris Lattner84e160a2007-05-19 07:03:17 +00001246 lhsType.getAsString(), rhsType.getAsString(),
1247 lex->getSourceRange(), rex->getSourceRange());
Steve Naroff1f4d7272007-05-11 04:00:31 +00001248 hadError = true;
1249 break;
Steve Naroff218bc2b2007-05-04 21:54:46 +00001250 case PointerFromInt:
1251 // check for null pointer constant (C99 6.3.2.3p3)
Chris Lattner0e9d6222007-07-15 23:26:56 +00001252 if (compoundType.isNull() && !rex->isNullPointerConstant(Context)) {
Steve Naroff56faab22007-05-30 04:20:12 +00001253 Diag(loc, diag::ext_typecheck_assign_pointer_int,
1254 lhsType.getAsString(), rhsType.getAsString(),
Steve Naroff9358c712007-05-27 23:58:33 +00001255 lex->getSourceRange(), rex->getSourceRange());
Steve Naroff9992bba2007-05-30 16:27:15 +00001256 }
Steve Naroff1f4d7272007-05-11 04:00:31 +00001257 break;
Steve Naroff56faab22007-05-30 04:20:12 +00001258 case IntFromPointer:
1259 Diag(loc, diag::ext_typecheck_assign_pointer_int,
1260 lhsType.getAsString(), rhsType.getAsString(),
Steve Naroff9358c712007-05-27 23:58:33 +00001261 lex->getSourceRange(), rex->getSourceRange());
Steve Naroff1f4d7272007-05-11 04:00:31 +00001262 break;
Steve Naroff218bc2b2007-05-04 21:54:46 +00001263 case IncompatiblePointer:
Steve Naroff9358c712007-05-27 23:58:33 +00001264 Diag(loc, diag::ext_typecheck_assign_incompatible_pointer,
1265 lhsType.getAsString(), rhsType.getAsString(),
1266 lex->getSourceRange(), rex->getSourceRange());
Steve Naroff1f4d7272007-05-11 04:00:31 +00001267 break;
1268 case CompatiblePointerDiscardsQualifiers:
Steve Naroff9358c712007-05-27 23:58:33 +00001269 Diag(loc, diag::ext_typecheck_assign_discards_qualifiers,
1270 lhsType.getAsString(), rhsType.getAsString(),
1271 lex->getSourceRange(), rex->getSourceRange());
Steve Naroff1f4d7272007-05-11 04:00:31 +00001272 break;
Steve Naroff218bc2b2007-05-04 21:54:46 +00001273 }
Steve Naroff98cf3e92007-06-06 18:38:38 +00001274 // C99 6.5.16p3: The type of an assignment expression is the type of the
1275 // left operand unless the left operand has qualified type, in which case
1276 // it is the unqualified version of the type of the left operand.
1277 // C99 6.5.16.1p2: In simple assignment, the value of the right operand
1278 // is converted to the type of the assignment expression (above).
1279 // C++ 5.17p1: the type of the assignment expression is that of its left oprdu.
1280 return hadError ? QualType() : lhsType.getUnqualifiedType();
Steve Naroffae4143e2007-04-26 20:39:23 +00001281}
1282
Steve Naroff218bc2b2007-05-04 21:54:46 +00001283inline QualType Sema::CheckCommaOperands( // C99 6.5.17
Steve Naroff7a5af782007-07-13 16:58:59 +00001284 Expr *&lex, Expr *&rex, SourceLocation loc) {
Steve Naroff31090012007-07-16 21:54:35 +00001285 UsualUnaryConversions(rex);
1286 return rex->getType();
Steve Naroff95af0132007-03-30 23:47:58 +00001287}
1288
Steve Naroff7a5af782007-07-13 16:58:59 +00001289/// CheckIncrementDecrementOperand - unlike most "Check" methods, this routine
1290/// doesn't need to call UsualUnaryConversions or UsualArithmeticConversions.
Steve Naroff71ce2e02007-05-18 22:53:50 +00001291QualType Sema::CheckIncrementDecrementOperand(Expr *op, SourceLocation OpLoc) {
Steve Naroff7a5af782007-07-13 16:58:59 +00001292 QualType resType = op->getType();
Steve Naroff35d85152007-05-07 00:24:15 +00001293 assert(!resType.isNull() && "no type for increment/decrement expression");
Steve Naroffd50c88e2007-04-05 21:15:20 +00001294
Steve Naroff9d139172007-08-24 17:20:07 +00001295 // C99 6.5.2.4p1: We allow complex as a GCC extension.
Steve Naroff35d85152007-05-07 00:24:15 +00001296 if (const PointerType *pt = dyn_cast<PointerType>(resType)) {
1297 if (!pt->getPointeeType()->isObjectType()) { // C99 6.5.2.4p2, 6.5.6p2
Steve Naroff71ce2e02007-05-18 22:53:50 +00001298 Diag(OpLoc, diag::err_typecheck_arithmetic_incomplete_type,
1299 resType.getAsString(), op->getSourceRange());
Steve Naroff35d85152007-05-07 00:24:15 +00001300 return QualType();
1301 }
Steve Naroff9d139172007-08-24 17:20:07 +00001302 } else if (!resType->isRealType()) {
1303 if (resType->isComplexType())
1304 // C99 does not support ++/-- on complex types.
1305 Diag(OpLoc, diag::ext_integer_increment_complex,
1306 resType.getAsString(), op->getSourceRange());
1307 else {
1308 Diag(OpLoc, diag::err_typecheck_illegal_increment_decrement,
1309 resType.getAsString(), op->getSourceRange());
1310 return QualType();
1311 }
Steve Naroff46ba1eb2007-04-03 23:13:13 +00001312 }
Steve Naroff9e1e5512007-08-23 21:37:33 +00001313 // At this point, we know we have a real, complex or pointer type.
1314 // Now make sure the operand is a modifiable lvalue.
Steve Naroff9358c712007-05-27 23:58:33 +00001315 Expr::isModifiableLvalueResult mlval = op->isModifiableLvalue();
1316 if (mlval != Expr::MLV_Valid) {
1317 // FIXME: emit a more precise diagnostic...
Steve Naroff71ce2e02007-05-18 22:53:50 +00001318 Diag(OpLoc, diag::err_typecheck_invalid_lvalue_incr_decr,
Chris Lattner84e160a2007-05-19 07:03:17 +00001319 op->getSourceRange());
Steve Naroff35d85152007-05-07 00:24:15 +00001320 return QualType();
1321 }
1322 return resType;
Steve Naroff26c8ea52007-03-21 21:08:52 +00001323}
1324
Steve Naroff1926c832007-04-24 00:23:05 +00001325/// getPrimaryDeclaration - Helper function for CheckAddressOfOperand().
Steve Naroff47500512007-04-19 23:00:49 +00001326/// This routine allows us to typecheck complex/recursive expressions
1327/// where the declaration is needed for type checking. Here are some
1328/// examples: &s.xx, &s.zz[1].yy, &(1+2), &(XX), &"123"[2].
Steve Naroff1926c832007-04-24 00:23:05 +00001329static Decl *getPrimaryDeclaration(Expr *e) {
Steve Naroff47500512007-04-19 23:00:49 +00001330 switch (e->getStmtClass()) {
1331 case Stmt::DeclRefExprClass:
1332 return cast<DeclRefExpr>(e)->getDecl();
1333 case Stmt::MemberExprClass:
Steve Naroff1926c832007-04-24 00:23:05 +00001334 return getPrimaryDeclaration(cast<MemberExpr>(e)->getBase());
Steve Naroff47500512007-04-19 23:00:49 +00001335 case Stmt::ArraySubscriptExprClass:
Steve Naroff1926c832007-04-24 00:23:05 +00001336 return getPrimaryDeclaration(cast<ArraySubscriptExpr>(e)->getBase());
Steve Naroff47500512007-04-19 23:00:49 +00001337 case Stmt::CallExprClass:
Steve Naroff1926c832007-04-24 00:23:05 +00001338 return getPrimaryDeclaration(cast<CallExpr>(e)->getCallee());
Steve Naroff47500512007-04-19 23:00:49 +00001339 case Stmt::UnaryOperatorClass:
Steve Naroff1926c832007-04-24 00:23:05 +00001340 return getPrimaryDeclaration(cast<UnaryOperator>(e)->getSubExpr());
Steve Naroff47500512007-04-19 23:00:49 +00001341 case Stmt::ParenExprClass:
Steve Naroff1926c832007-04-24 00:23:05 +00001342 return getPrimaryDeclaration(cast<ParenExpr>(e)->getSubExpr());
Steve Naroff47500512007-04-19 23:00:49 +00001343 default:
1344 return 0;
1345 }
1346}
1347
1348/// CheckAddressOfOperand - The operand of & must be either a function
1349/// designator or an lvalue designating an object. If it is an lvalue, the
1350/// object cannot be declared with storage class register or be a bit field.
1351/// Note: The usual conversions are *not* applied to the operand of the &
Steve Naroffa78fe7e2007-05-16 19:47:19 +00001352/// operator (C99 6.3.2.1p[2-4]), and its result is never an lvalue.
Steve Naroff35d85152007-05-07 00:24:15 +00001353QualType Sema::CheckAddressOfOperand(Expr *op, SourceLocation OpLoc) {
Steve Naroff1926c832007-04-24 00:23:05 +00001354 Decl *dcl = getPrimaryDeclaration(op);
Steve Naroff9358c712007-05-27 23:58:33 +00001355 Expr::isLvalueResult lval = op->isLvalue();
Steve Naroff47500512007-04-19 23:00:49 +00001356
Steve Naroff9358c712007-05-27 23:58:33 +00001357 if (lval != Expr::LV_Valid) { // C99 6.5.3.2p1
Steve Naroff5dd642e2007-05-14 18:14:51 +00001358 if (dcl && isa<FunctionDecl>(dcl)) // allow function designators
1359 ;
Steve Naroff9358c712007-05-27 23:58:33 +00001360 else { // FIXME: emit more specific diag...
Steve Naroff71ce2e02007-05-18 22:53:50 +00001361 Diag(OpLoc, diag::err_typecheck_invalid_lvalue_addrof,
1362 op->getSourceRange());
Steve Naroff35d85152007-05-07 00:24:15 +00001363 return QualType();
1364 }
Steve Naroff47500512007-04-19 23:00:49 +00001365 } else if (dcl) {
1366 // We have an lvalue with a decl. Make sure the decl is not declared
1367 // with the register storage-class specifier.
1368 if (const VarDecl *vd = dyn_cast<VarDecl>(dcl)) {
Steve Naroff35d85152007-05-07 00:24:15 +00001369 if (vd->getStorageClass() == VarDecl::Register) {
Steve Naroff71ce2e02007-05-18 22:53:50 +00001370 Diag(OpLoc, diag::err_typecheck_address_of_register,
Chris Lattner84e160a2007-05-19 07:03:17 +00001371 op->getSourceRange());
Steve Naroff35d85152007-05-07 00:24:15 +00001372 return QualType();
1373 }
Steve Narofff633d092007-04-25 19:01:39 +00001374 } else
1375 assert(0 && "Unknown/unexpected decl type");
1376
Steve Naroff47500512007-04-19 23:00:49 +00001377 // FIXME: add check for bitfields!
1378 }
1379 // If the operand has type "type", the result has type "pointer to type".
Steve Naroff35d85152007-05-07 00:24:15 +00001380 return Context.getPointerType(op->getType());
Steve Naroff47500512007-04-19 23:00:49 +00001381}
1382
Steve Naroff35d85152007-05-07 00:24:15 +00001383QualType Sema::CheckIndirectionOperand(Expr *op, SourceLocation OpLoc) {
Steve Naroff31090012007-07-16 21:54:35 +00001384 UsualUnaryConversions(op);
1385 QualType qType = op->getType();
Steve Naroff35d85152007-05-07 00:24:15 +00001386
Chris Lattnerc996b172007-07-31 16:53:04 +00001387 if (const PointerType *PT = qType->getAsPointerType()) {
Steve Naroff758ada12007-05-28 16:15:57 +00001388 QualType ptype = PT->getPointeeType();
1389 // C99 6.5.3.2p4. "if it points to an object,...".
1390 if (ptype->isIncompleteType()) { // An incomplete type is not an object
1391 // GCC compat: special case 'void *' (treat as warning).
1392 if (ptype->isVoidType()) {
1393 Diag(OpLoc, diag::ext_typecheck_deref_ptr_to_void,
Steve Naroffeb9da942007-05-30 00:06:37 +00001394 qType.getAsString(), op->getSourceRange());
Steve Naroff758ada12007-05-28 16:15:57 +00001395 } else {
1396 Diag(OpLoc, diag::err_typecheck_deref_incomplete_type,
Steve Naroffeb9da942007-05-30 00:06:37 +00001397 ptype.getAsString(), op->getSourceRange());
Steve Naroff758ada12007-05-28 16:15:57 +00001398 return QualType();
1399 }
1400 }
1401 return ptype;
1402 }
1403 Diag(OpLoc, diag::err_typecheck_indirection_requires_pointer,
Steve Naroffeb9da942007-05-30 00:06:37 +00001404 qType.getAsString(), op->getSourceRange());
Steve Naroff35d85152007-05-07 00:24:15 +00001405 return QualType();
Steve Naroff1926c832007-04-24 00:23:05 +00001406}
Steve Naroff218bc2b2007-05-04 21:54:46 +00001407
1408static inline BinaryOperator::Opcode ConvertTokenKindToBinaryOpcode(
1409 tok::TokenKind Kind) {
1410 BinaryOperator::Opcode Opc;
1411 switch (Kind) {
1412 default: assert(0 && "Unknown binop!");
1413 case tok::star: Opc = BinaryOperator::Mul; break;
1414 case tok::slash: Opc = BinaryOperator::Div; break;
1415 case tok::percent: Opc = BinaryOperator::Rem; break;
1416 case tok::plus: Opc = BinaryOperator::Add; break;
1417 case tok::minus: Opc = BinaryOperator::Sub; break;
1418 case tok::lessless: Opc = BinaryOperator::Shl; break;
1419 case tok::greatergreater: Opc = BinaryOperator::Shr; break;
1420 case tok::lessequal: Opc = BinaryOperator::LE; break;
1421 case tok::less: Opc = BinaryOperator::LT; break;
1422 case tok::greaterequal: Opc = BinaryOperator::GE; break;
1423 case tok::greater: Opc = BinaryOperator::GT; break;
1424 case tok::exclaimequal: Opc = BinaryOperator::NE; break;
1425 case tok::equalequal: Opc = BinaryOperator::EQ; break;
1426 case tok::amp: Opc = BinaryOperator::And; break;
1427 case tok::caret: Opc = BinaryOperator::Xor; break;
1428 case tok::pipe: Opc = BinaryOperator::Or; break;
1429 case tok::ampamp: Opc = BinaryOperator::LAnd; break;
1430 case tok::pipepipe: Opc = BinaryOperator::LOr; break;
1431 case tok::equal: Opc = BinaryOperator::Assign; break;
1432 case tok::starequal: Opc = BinaryOperator::MulAssign; break;
1433 case tok::slashequal: Opc = BinaryOperator::DivAssign; break;
1434 case tok::percentequal: Opc = BinaryOperator::RemAssign; break;
1435 case tok::plusequal: Opc = BinaryOperator::AddAssign; break;
1436 case tok::minusequal: Opc = BinaryOperator::SubAssign; break;
1437 case tok::lesslessequal: Opc = BinaryOperator::ShlAssign; break;
1438 case tok::greatergreaterequal: Opc = BinaryOperator::ShrAssign; break;
1439 case tok::ampequal: Opc = BinaryOperator::AndAssign; break;
1440 case tok::caretequal: Opc = BinaryOperator::XorAssign; break;
1441 case tok::pipeequal: Opc = BinaryOperator::OrAssign; break;
1442 case tok::comma: Opc = BinaryOperator::Comma; break;
1443 }
1444 return Opc;
1445}
1446
Steve Naroff35d85152007-05-07 00:24:15 +00001447static inline UnaryOperator::Opcode ConvertTokenKindToUnaryOpcode(
1448 tok::TokenKind Kind) {
1449 UnaryOperator::Opcode Opc;
1450 switch (Kind) {
1451 default: assert(0 && "Unknown unary op!");
1452 case tok::plusplus: Opc = UnaryOperator::PreInc; break;
1453 case tok::minusminus: Opc = UnaryOperator::PreDec; break;
1454 case tok::amp: Opc = UnaryOperator::AddrOf; break;
1455 case tok::star: Opc = UnaryOperator::Deref; break;
1456 case tok::plus: Opc = UnaryOperator::Plus; break;
1457 case tok::minus: Opc = UnaryOperator::Minus; break;
1458 case tok::tilde: Opc = UnaryOperator::Not; break;
1459 case tok::exclaim: Opc = UnaryOperator::LNot; break;
1460 case tok::kw_sizeof: Opc = UnaryOperator::SizeOf; break;
1461 case tok::kw___alignof: Opc = UnaryOperator::AlignOf; break;
1462 case tok::kw___real: Opc = UnaryOperator::Real; break;
1463 case tok::kw___imag: Opc = UnaryOperator::Imag; break;
Chris Lattnerd0f76512007-06-08 22:16:53 +00001464 case tok::kw___extension__: Opc = UnaryOperator::Extension; break;
Steve Naroff35d85152007-05-07 00:24:15 +00001465 }
1466 return Opc;
1467}
1468
Steve Naroff218bc2b2007-05-04 21:54:46 +00001469// Binary Operators. 'Tok' is the token for the operator.
1470Action::ExprResult Sema::ParseBinOp(SourceLocation TokLoc, tok::TokenKind Kind,
1471 ExprTy *LHS, ExprTy *RHS) {
1472 BinaryOperator::Opcode Opc = ConvertTokenKindToBinaryOpcode(Kind);
1473 Expr *lhs = (Expr *)LHS, *rhs = (Expr*)RHS;
1474
1475 assert((lhs != 0) && "ParseBinOp(): missing left expression");
1476 assert((rhs != 0) && "ParseBinOp(): missing right expression");
1477
Chris Lattner256c21b2007-06-28 03:53:10 +00001478 QualType ResultTy; // Result type of the binary operator.
1479 QualType CompTy; // Computation type for compound assignments (e.g. '+=')
Steve Naroff218bc2b2007-05-04 21:54:46 +00001480
1481 switch (Opc) {
1482 default:
1483 assert(0 && "Unknown binary expr!");
1484 case BinaryOperator::Assign:
Chris Lattner256c21b2007-06-28 03:53:10 +00001485 ResultTy = CheckAssignmentOperands(lhs, rhs, TokLoc, QualType());
Steve Naroff218bc2b2007-05-04 21:54:46 +00001486 break;
1487 case BinaryOperator::Mul:
1488 case BinaryOperator::Div:
Chris Lattner256c21b2007-06-28 03:53:10 +00001489 ResultTy = CheckMultiplyDivideOperands(lhs, rhs, TokLoc);
Steve Naroff218bc2b2007-05-04 21:54:46 +00001490 break;
1491 case BinaryOperator::Rem:
Chris Lattner256c21b2007-06-28 03:53:10 +00001492 ResultTy = CheckRemainderOperands(lhs, rhs, TokLoc);
Steve Naroff218bc2b2007-05-04 21:54:46 +00001493 break;
1494 case BinaryOperator::Add:
Chris Lattner256c21b2007-06-28 03:53:10 +00001495 ResultTy = CheckAdditionOperands(lhs, rhs, TokLoc);
Steve Naroff218bc2b2007-05-04 21:54:46 +00001496 break;
1497 case BinaryOperator::Sub:
Chris Lattner256c21b2007-06-28 03:53:10 +00001498 ResultTy = CheckSubtractionOperands(lhs, rhs, TokLoc);
Steve Naroff218bc2b2007-05-04 21:54:46 +00001499 break;
1500 case BinaryOperator::Shl:
1501 case BinaryOperator::Shr:
Chris Lattner256c21b2007-06-28 03:53:10 +00001502 ResultTy = CheckShiftOperands(lhs, rhs, TokLoc);
Steve Naroff218bc2b2007-05-04 21:54:46 +00001503 break;
1504 case BinaryOperator::LE:
1505 case BinaryOperator::LT:
1506 case BinaryOperator::GE:
1507 case BinaryOperator::GT:
Chris Lattnerb620c342007-08-26 01:18:55 +00001508 ResultTy = CheckCompareOperands(lhs, rhs, TokLoc, true);
Steve Naroff218bc2b2007-05-04 21:54:46 +00001509 break;
1510 case BinaryOperator::EQ:
1511 case BinaryOperator::NE:
Chris Lattnerb620c342007-08-26 01:18:55 +00001512 ResultTy = CheckCompareOperands(lhs, rhs, TokLoc, false);
Steve Naroff218bc2b2007-05-04 21:54:46 +00001513 break;
1514 case BinaryOperator::And:
1515 case BinaryOperator::Xor:
1516 case BinaryOperator::Or:
Chris Lattner256c21b2007-06-28 03:53:10 +00001517 ResultTy = CheckBitwiseOperands(lhs, rhs, TokLoc);
Steve Naroff218bc2b2007-05-04 21:54:46 +00001518 break;
1519 case BinaryOperator::LAnd:
1520 case BinaryOperator::LOr:
Chris Lattner256c21b2007-06-28 03:53:10 +00001521 ResultTy = CheckLogicalOperands(lhs, rhs, TokLoc);
Steve Naroff218bc2b2007-05-04 21:54:46 +00001522 break;
1523 case BinaryOperator::MulAssign:
1524 case BinaryOperator::DivAssign:
Steve Naroffbe4c4d12007-08-24 19:07:16 +00001525 CompTy = CheckMultiplyDivideOperands(lhs, rhs, TokLoc, true);
Chris Lattner256c21b2007-06-28 03:53:10 +00001526 if (!CompTy.isNull())
1527 ResultTy = CheckAssignmentOperands(lhs, rhs, TokLoc, CompTy);
Steve Naroff218bc2b2007-05-04 21:54:46 +00001528 break;
1529 case BinaryOperator::RemAssign:
Steve Naroffbe4c4d12007-08-24 19:07:16 +00001530 CompTy = CheckRemainderOperands(lhs, rhs, TokLoc, true);
Chris Lattner256c21b2007-06-28 03:53:10 +00001531 if (!CompTy.isNull())
1532 ResultTy = CheckAssignmentOperands(lhs, rhs, TokLoc, CompTy);
Steve Naroff218bc2b2007-05-04 21:54:46 +00001533 break;
1534 case BinaryOperator::AddAssign:
Steve Naroffbe4c4d12007-08-24 19:07:16 +00001535 CompTy = CheckAdditionOperands(lhs, rhs, TokLoc, true);
Chris Lattner256c21b2007-06-28 03:53:10 +00001536 if (!CompTy.isNull())
1537 ResultTy = CheckAssignmentOperands(lhs, rhs, TokLoc, CompTy);
Steve Naroff218bc2b2007-05-04 21:54:46 +00001538 break;
1539 case BinaryOperator::SubAssign:
Steve Naroffbe4c4d12007-08-24 19:07:16 +00001540 CompTy = CheckSubtractionOperands(lhs, rhs, TokLoc, true);
Chris Lattner256c21b2007-06-28 03:53:10 +00001541 if (!CompTy.isNull())
1542 ResultTy = CheckAssignmentOperands(lhs, rhs, TokLoc, CompTy);
Steve Naroff218bc2b2007-05-04 21:54:46 +00001543 break;
1544 case BinaryOperator::ShlAssign:
1545 case BinaryOperator::ShrAssign:
Steve Naroffbe4c4d12007-08-24 19:07:16 +00001546 CompTy = CheckShiftOperands(lhs, rhs, TokLoc, true);
Chris Lattner256c21b2007-06-28 03:53:10 +00001547 if (!CompTy.isNull())
1548 ResultTy = CheckAssignmentOperands(lhs, rhs, TokLoc, CompTy);
Steve Naroff218bc2b2007-05-04 21:54:46 +00001549 break;
1550 case BinaryOperator::AndAssign:
1551 case BinaryOperator::XorAssign:
1552 case BinaryOperator::OrAssign:
Steve Naroffbe4c4d12007-08-24 19:07:16 +00001553 CompTy = CheckBitwiseOperands(lhs, rhs, TokLoc, true);
Chris Lattner256c21b2007-06-28 03:53:10 +00001554 if (!CompTy.isNull())
1555 ResultTy = CheckAssignmentOperands(lhs, rhs, TokLoc, CompTy);
Steve Naroff218bc2b2007-05-04 21:54:46 +00001556 break;
1557 case BinaryOperator::Comma:
Chris Lattner256c21b2007-06-28 03:53:10 +00001558 ResultTy = CheckCommaOperands(lhs, rhs, TokLoc);
Steve Naroff218bc2b2007-05-04 21:54:46 +00001559 break;
1560 }
Chris Lattner256c21b2007-06-28 03:53:10 +00001561 if (ResultTy.isNull())
Steve Naroff218bc2b2007-05-04 21:54:46 +00001562 return true;
Chris Lattner256c21b2007-06-28 03:53:10 +00001563 if (CompTy.isNull())
1564 return new BinaryOperator(lhs, rhs, Opc, ResultTy);
1565 else
Chris Lattner9369a562007-06-29 16:31:29 +00001566 return new CompoundAssignOperator(lhs, rhs, Opc, ResultTy, CompTy);
Steve Naroff218bc2b2007-05-04 21:54:46 +00001567}
1568
Steve Naroff35d85152007-05-07 00:24:15 +00001569// Unary Operators. 'Tok' is the token for the operator.
1570Action::ExprResult Sema::ParseUnaryOp(SourceLocation OpLoc, tok::TokenKind Op,
Chris Lattner86554282007-06-08 22:32:33 +00001571 ExprTy *input) {
1572 Expr *Input = (Expr*)input;
Steve Naroff35d85152007-05-07 00:24:15 +00001573 UnaryOperator::Opcode Opc = ConvertTokenKindToUnaryOpcode(Op);
1574 QualType resultType;
1575 switch (Opc) {
1576 default:
1577 assert(0 && "Unimplemented unary expr!");
1578 case UnaryOperator::PreInc:
1579 case UnaryOperator::PreDec:
Chris Lattner86554282007-06-08 22:32:33 +00001580 resultType = CheckIncrementDecrementOperand(Input, OpLoc);
Steve Naroff35d85152007-05-07 00:24:15 +00001581 break;
1582 case UnaryOperator::AddrOf:
Chris Lattner86554282007-06-08 22:32:33 +00001583 resultType = CheckAddressOfOperand(Input, OpLoc);
Steve Naroff35d85152007-05-07 00:24:15 +00001584 break;
1585 case UnaryOperator::Deref:
Chris Lattner86554282007-06-08 22:32:33 +00001586 resultType = CheckIndirectionOperand(Input, OpLoc);
Steve Naroff35d85152007-05-07 00:24:15 +00001587 break;
1588 case UnaryOperator::Plus:
1589 case UnaryOperator::Minus:
Steve Naroff31090012007-07-16 21:54:35 +00001590 UsualUnaryConversions(Input);
1591 resultType = Input->getType();
Steve Naroff35d85152007-05-07 00:24:15 +00001592 if (!resultType->isArithmeticType()) // C99 6.5.3.3p1
Chris Lattnerc04bd6a2007-05-16 18:09:54 +00001593 return Diag(OpLoc, diag::err_typecheck_unary_expr,
1594 resultType.getAsString());
Steve Naroff35d85152007-05-07 00:24:15 +00001595 break;
1596 case UnaryOperator::Not: // bitwise complement
Steve Naroff31090012007-07-16 21:54:35 +00001597 UsualUnaryConversions(Input);
1598 resultType = Input->getType();
Steve Naroff9d139172007-08-24 17:20:07 +00001599 // C99 6.5.3.3p1. We allow complex as a GCC extension.
1600 if (!resultType->isIntegerType()) {
1601 if (resultType->isComplexType())
1602 // C99 does not support '~' for complex conjugation.
1603 Diag(OpLoc, diag::ext_integer_complement_complex,
1604 resultType.getAsString());
1605 else
1606 return Diag(OpLoc, diag::err_typecheck_unary_expr,
1607 resultType.getAsString());
1608 }
Steve Naroff35d85152007-05-07 00:24:15 +00001609 break;
1610 case UnaryOperator::LNot: // logical negation
Steve Naroff71b59a92007-06-04 22:22:31 +00001611 // Unlike +/-/~, integer promotions aren't done here (C99 6.5.3.3p5).
Steve Naroff31090012007-07-16 21:54:35 +00001612 DefaultFunctionArrayConversion(Input);
1613 resultType = Input->getType();
Steve Naroff35d85152007-05-07 00:24:15 +00001614 if (!resultType->isScalarType()) // C99 6.5.3.3p1
Chris Lattnerc04bd6a2007-05-16 18:09:54 +00001615 return Diag(OpLoc, diag::err_typecheck_unary_expr,
1616 resultType.getAsString());
Chris Lattnerbe31ed82007-06-02 19:11:33 +00001617 // LNot always has type int. C99 6.5.3.3p5.
1618 resultType = Context.IntTy;
Steve Naroff35d85152007-05-07 00:24:15 +00001619 break;
1620 case UnaryOperator::SizeOf:
Chris Lattner86554282007-06-08 22:32:33 +00001621 resultType = CheckSizeOfAlignOfOperand(Input->getType(), OpLoc, true);
Steve Naroff043d45d2007-05-15 02:32:35 +00001622 break;
Steve Naroff35d85152007-05-07 00:24:15 +00001623 case UnaryOperator::AlignOf:
Chris Lattner86554282007-06-08 22:32:33 +00001624 resultType = CheckSizeOfAlignOfOperand(Input->getType(), OpLoc, false);
1625 break;
Chris Lattner30b5dd02007-08-24 21:16:53 +00001626 case UnaryOperator::Real:
Chris Lattner30b5dd02007-08-24 21:16:53 +00001627 case UnaryOperator::Imag:
Chris Lattner74ed76b2007-08-24 21:41:10 +00001628 resultType = CheckRealImagOperand(Input, OpLoc);
Chris Lattner30b5dd02007-08-24 21:16:53 +00001629 break;
Chris Lattner86554282007-06-08 22:32:33 +00001630 case UnaryOperator::Extension:
Chris Lattner86554282007-06-08 22:32:33 +00001631 resultType = Input->getType();
Steve Naroff043d45d2007-05-15 02:32:35 +00001632 break;
Steve Naroff35d85152007-05-07 00:24:15 +00001633 }
1634 if (resultType.isNull())
1635 return true;
Chris Lattner86554282007-06-08 22:32:33 +00001636 return new UnaryOperator(Input, Opc, resultType, OpLoc);
Steve Naroff35d85152007-05-07 00:24:15 +00001637}
Chris Lattnereefa10e2007-05-28 06:56:27 +00001638
1639/// ParseAddrLabel - Parse the GNU address of label extension: "&&foo".
1640Sema::ExprResult Sema::ParseAddrLabel(SourceLocation OpLoc,
1641 SourceLocation LabLoc,
1642 IdentifierInfo *LabelII) {
1643 // Look up the record for this label identifier.
1644 LabelStmt *&LabelDecl = LabelMap[LabelII];
1645
1646 // If we haven't seen this label yet, create a forward reference.
1647 if (LabelDecl == 0)
1648 LabelDecl = new LabelStmt(LabLoc, LabelII, 0);
1649
1650 // Create the AST node. The address of a label always has type 'void*'.
Chris Lattnerd268a7a2007-08-03 17:31:20 +00001651 return new AddrLabelExpr(OpLoc, LabLoc, LabelDecl,
1652 Context.getPointerType(Context.VoidTy));
Chris Lattnereefa10e2007-05-28 06:56:27 +00001653}
1654
Chris Lattner366727f2007-07-24 16:58:17 +00001655Sema::ExprResult Sema::ParseStmtExpr(SourceLocation LPLoc, StmtTy *substmt,
1656 SourceLocation RPLoc) { // "({..})"
1657 Stmt *SubStmt = static_cast<Stmt*>(substmt);
1658 assert(SubStmt && isa<CompoundStmt>(SubStmt) && "Invalid action invocation!");
1659 CompoundStmt *Compound = cast<CompoundStmt>(SubStmt);
1660
1661 // FIXME: there are a variety of strange constraints to enforce here, for
1662 // example, it is not possible to goto into a stmt expression apparently.
1663 // More semantic analysis is needed.
1664
1665 // FIXME: the last statement in the compount stmt has its value used. We
1666 // should not warn about it being unused.
1667
1668 // If there are sub stmts in the compound stmt, take the type of the last one
1669 // as the type of the stmtexpr.
1670 QualType Ty = Context.VoidTy;
1671
1672 if (!Compound->body_empty())
1673 if (Expr *LastExpr = dyn_cast<Expr>(Compound->body_back()))
1674 Ty = LastExpr->getType();
1675
1676 return new StmtExpr(Compound, Ty, LPLoc, RPLoc);
1677}
Steve Naroff78864672007-08-01 22:05:33 +00001678
Steve Naroff788d8642007-08-01 23:45:51 +00001679Sema::ExprResult Sema::ParseTypesCompatibleExpr(SourceLocation BuiltinLoc,
Steve Naroff78864672007-08-01 22:05:33 +00001680 TypeTy *arg1, TypeTy *arg2,
1681 SourceLocation RPLoc) {
1682 QualType argT1 = QualType::getFromOpaquePtr(arg1);
1683 QualType argT2 = QualType::getFromOpaquePtr(arg2);
1684
1685 assert((!argT1.isNull() && !argT2.isNull()) && "Missing type argument(s)");
1686
Steve Naroff788d8642007-08-01 23:45:51 +00001687 return new TypesCompatibleExpr(Context.IntTy, BuiltinLoc, argT1, argT2, RPLoc);
Steve Naroff78864672007-08-01 22:05:33 +00001688}
1689
Steve Naroff9efdabc2007-08-03 21:21:27 +00001690Sema::ExprResult Sema::ParseChooseExpr(SourceLocation BuiltinLoc, ExprTy *cond,
1691 ExprTy *expr1, ExprTy *expr2,
1692 SourceLocation RPLoc) {
1693 Expr *CondExpr = static_cast<Expr*>(cond);
1694 Expr *LHSExpr = static_cast<Expr*>(expr1);
1695 Expr *RHSExpr = static_cast<Expr*>(expr2);
1696
1697 assert((CondExpr && LHSExpr && RHSExpr) && "Missing type argument(s)");
1698
1699 // The conditional expression is required to be a constant expression.
1700 llvm::APSInt condEval(32);
1701 SourceLocation ExpLoc;
1702 if (!CondExpr->isIntegerConstantExpr(condEval, Context, &ExpLoc))
1703 return Diag(ExpLoc, diag::err_typecheck_choose_expr_requires_constant,
1704 CondExpr->getSourceRange());
1705
1706 // If the condition is > zero, then the AST type is the same as the LSHExpr.
1707 QualType resType = condEval.getZExtValue() ? LHSExpr->getType() :
1708 RHSExpr->getType();
1709 return new ChooseExpr(BuiltinLoc, CondExpr, LHSExpr, RHSExpr, resType, RPLoc);
1710}
1711
Anders Carlsson76f4a902007-08-21 17:43:55 +00001712// TODO: Move this to SemaObjC.cpp
Anders Carlssonc5a81eb2007-08-22 15:14:15 +00001713Sema::ExprResult Sema::ParseObjCStringLiteral(ExprTy *string) {
Anders Carlsson76f4a902007-08-21 17:43:55 +00001714 StringLiteral* S = static_cast<StringLiteral *>(string);
1715
1716 if (CheckBuiltinCFStringArgument(S))
1717 return true;
1718
1719 QualType t = Context.getCFConstantStringType();
1720 t = t.getQualifiedType(QualType::Const);
1721 t = Context.getPointerType(t);
1722
1723 return new ObjCStringLiteral(S, t);
1724}
Anders Carlssonc5a81eb2007-08-22 15:14:15 +00001725
1726Sema::ExprResult Sema::ParseObjCEncodeExpression(SourceLocation AtLoc,
1727 SourceLocation LParenLoc,
1728 TypeTy *Ty,
1729 SourceLocation RParenLoc) {
1730 QualType EncodedType = QualType::getFromOpaquePtr(Ty);
1731
1732 QualType t = Context.getPointerType(Context.CharTy);
1733 return new ObjCEncodeExpr(t, EncodedType, AtLoc, RParenLoc);
1734}