blob: fc05189d23d626c91ab8d7cfab1ad38b691ef493 [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"
Steve Naroff17f76e02007-05-03 21:03:48 +000025#include "llvm/ADT/StringExtras.h"
Chris Lattner5b183d82006-11-10 05:03:26 +000026using namespace llvm;
27using namespace clang;
28
Steve Naroffdf7855b2007-02-21 23:46:25 +000029/// ParseStringLiteral - The specified tokens were lexed as pasted string
Chris Lattner5b183d82006-11-10 05:03:26 +000030/// fragments (e.g. "foo" "bar" L"baz"). The result string has to handle string
31/// concatenation ([C99 5.1.1.2, translation phase #6]), so it may come from
32/// multiple tokens. However, the common case is that StringToks points to one
33/// string.
34///
35Action::ExprResult
Steve Naroffdf7855b2007-02-21 23:46:25 +000036Sema::ParseStringLiteral(const LexerToken *StringToks, unsigned NumStringToks) {
Chris Lattner5b183d82006-11-10 05:03:26 +000037 assert(NumStringToks && "Must have at least one string!");
38
Steve Naroff4f88b312007-03-13 22:37:02 +000039 StringLiteralParser Literal(StringToks, NumStringToks, PP, Context.Target);
40 if (Literal.hadError)
41 return ExprResult(true);
Chris Lattner5b183d82006-11-10 05:03:26 +000042
Chris Lattner5b183d82006-11-10 05:03:26 +000043 SmallVector<SourceLocation, 4> StringTokLocs;
44 for (unsigned i = 0; i != NumStringToks; ++i)
45 StringTokLocs.push_back(StringToks[i].getLocation());
Steve Narofff1e53692007-03-23 22:27:02 +000046
47 // FIXME: handle wchar_t
Steve Naroffe5aa9be2007-04-05 22:36:20 +000048 QualType t = Context.getPointerType(Context.CharTy);
Steve Narofff1e53692007-03-23 22:27:02 +000049
Chris Lattner5b183d82006-11-10 05:03:26 +000050 // FIXME: use factory.
Chris Lattner5b183d82006-11-10 05:03:26 +000051 // Pass &StringTokLocs[0], StringTokLocs.size() to factory!
Steve Naroff4f88b312007-03-13 22:37:02 +000052 return new StringLiteral(Literal.GetString(), Literal.GetStringLength(),
Steve Naroff53f07dc2007-05-17 21:49:33 +000053 Literal.AnyWide, t, StringToks[0].getLocation(),
54 StringToks[NumStringToks-1].getLocation());
Chris Lattner5b183d82006-11-10 05:03:26 +000055}
56
Chris Lattnere168f762006-11-10 05:29:30 +000057
Chris Lattnerac18be92006-11-20 06:49:47 +000058/// ParseIdentifierExpr - The parser read an identifier in expression context,
59/// validate it per-C99 6.5.1. HasTrailingLParen indicates whether this
60/// identifier is used in an function call context.
61Sema::ExprResult Sema::ParseIdentifierExpr(Scope *S, SourceLocation Loc,
62 IdentifierInfo &II,
63 bool HasTrailingLParen) {
Chris Lattner17ed4872006-11-20 04:58:19 +000064 // Could be enum-constant or decl.
Chris Lattner9561a0b2007-01-28 08:20:04 +000065 Decl *D = LookupScopedDecl(&II, Decl::IDNS_Ordinary, Loc, S);
Chris Lattner17ed4872006-11-20 04:58:19 +000066 if (D == 0) {
Bill Wendling4073ed52007-02-13 01:51:42 +000067 // Otherwise, this could be an implicitly declared function reference (legal
Chris Lattner9561a0b2007-01-28 08:20:04 +000068 // in C90, extension in C99).
Chris Lattnerac18be92006-11-20 06:49:47 +000069 if (HasTrailingLParen &&
70 // Not in C++.
Steve Narofff1e53692007-03-23 22:27:02 +000071 !getLangOptions().CPlusPlus)
Chris Lattnerac18be92006-11-20 06:49:47 +000072 D = ImplicitlyDefineFunction(Loc, II, S);
Steve Naroff92e30f82007-04-02 22:35:25 +000073 else {
Chris Lattnerac18be92006-11-20 06:49:47 +000074 // If this name wasn't predeclared and if this is not a function call,
75 // diagnose the problem.
Steve Narofff1e53692007-03-23 22:27:02 +000076 return Diag(Loc, diag::err_undeclared_var_use, II.getName());
Steve Naroff92e30f82007-04-02 22:35:25 +000077 }
Chris Lattner17ed4872006-11-20 04:58:19 +000078 }
79
Steve Naroff46ba1eb2007-04-03 23:13:13 +000080 if (ValueDecl *VD = dyn_cast<ValueDecl>(D))
Steve Naroff509fe022007-05-17 01:16:00 +000081 return new DeclRefExpr(VD, VD->getType(), Loc);
Steve Naroff46ba1eb2007-04-03 23:13:13 +000082 if (isa<TypedefDecl>(D))
Steve Narofff1e53692007-03-23 22:27:02 +000083 return Diag(Loc, diag::err_unexpected_typedef, II.getName());
84
85 assert(0 && "Invalid decl");
Chris Lattner17ed4872006-11-20 04:58:19 +000086}
Chris Lattnere168f762006-11-10 05:29:30 +000087
Chris Lattner17ed4872006-11-20 04:58:19 +000088Sema::ExprResult Sema::ParseSimplePrimaryExpr(SourceLocation Loc,
89 tok::TokenKind Kind) {
Chris Lattnere168f762006-11-10 05:29:30 +000090 switch (Kind) {
91 default:
92 assert(0 && "Unknown simple primary expr!");
Steve Naroffe4718892007-04-27 18:30:00 +000093 // TODO: MOVE this to be some other callback.
Chris Lattnere168f762006-11-10 05:29:30 +000094 case tok::kw___func__: // primary-expression: __func__ [C99 6.4.2.2]
95 case tok::kw___FUNCTION__: // primary-expression: __FUNCTION__ [GNU]
96 case tok::kw___PRETTY_FUNCTION__: // primary-expression: __P..Y_F..N__ [GNU]
Chris Lattner17ed4872006-11-20 04:58:19 +000097 return 0;
Chris Lattnere168f762006-11-10 05:29:30 +000098 }
99}
100
Steve Naroffae4143e2007-04-26 20:39:23 +0000101Sema::ExprResult Sema::ParseCharacterConstant(const LexerToken &Tok) {
102 SmallString<16> CharBuffer;
103 CharBuffer.resize(Tok.getLength());
104 const char *ThisTokBegin = &CharBuffer[0];
105 unsigned ActualLength = PP.getSpelling(Tok, ThisTokBegin);
106
107 CharLiteralParser Literal(ThisTokBegin, ThisTokBegin+ActualLength,
108 Tok.getLocation(), PP);
109 if (Literal.hadError())
110 return ExprResult(true);
Steve Naroff509fe022007-05-17 01:16:00 +0000111 return new CharacterLiteral(Literal.getValue(), Context.IntTy,
112 Tok.getLocation());
Steve Naroffae4143e2007-04-26 20:39:23 +0000113}
114
Steve Naroff8160ea22007-03-06 01:09:46 +0000115Action::ExprResult Sema::ParseNumericConstant(const LexerToken &Tok) {
Steve Narofff2fb89e2007-03-13 20:29:44 +0000116 // fast path for a single digit (which is quite common). A single digit
117 // cannot have a trigraph, escaped newline, radix prefix, or type suffix.
118 if (Tok.getLength() == 1) {
119 const char *t = PP.getSourceManager().getCharacterData(Tok.getLocation());
Chris Lattner67ca9252007-05-21 01:08:44 +0000120
121 unsigned IntSize = Context.Target.getIntWidth(Tok.getLocation());
122 return ExprResult(new IntegerLiteral(APInt(IntSize, *t-'0'), Context.IntTy,
Steve Naroff509fe022007-05-17 01:16:00 +0000123 Tok.getLocation()));
Steve Narofff2fb89e2007-03-13 20:29:44 +0000124 }
Steve Naroff8160ea22007-03-06 01:09:46 +0000125 SmallString<512> IntegerBuffer;
126 IntegerBuffer.resize(Tok.getLength());
127 const char *ThisTokBegin = &IntegerBuffer[0];
128
Chris Lattner67ca9252007-05-21 01:08:44 +0000129 // Get the spelling of the token, which eliminates trigraphs, etc.
Steve Naroff8160ea22007-03-06 01:09:46 +0000130 unsigned ActualLength = PP.getSpelling(Tok, ThisTokBegin);
Steve Naroff09ef4742007-03-09 23:16:33 +0000131 NumericLiteralParser Literal(ThisTokBegin, ThisTokBegin+ActualLength,
Steve Naroff451d8f162007-03-12 23:22:38 +0000132 Tok.getLocation(), PP);
Steve Narofff2fb89e2007-03-13 20:29:44 +0000133 if (Literal.hadError)
134 return ExprResult(true);
Chris Lattner67ca9252007-05-21 01:08:44 +0000135
Steve Naroff09ef4742007-03-09 23:16:33 +0000136 if (Literal.isIntegerLiteral()) {
Steve Naroffe5aa9be2007-04-05 22:36:20 +0000137 QualType t;
Chris Lattner67ca9252007-05-21 01:08:44 +0000138
139 // Get the value in the widest-possible width.
140 APInt ResultVal(Context.Target.getIntMaxTWidth(Tok.getLocation()), 0);
141
142 if (Literal.GetIntegerValue(ResultVal)) {
143 // If this value didn't fit into uintmax_t, warn and force to ull.
144 Diag(Tok.getLocation(), diag::warn_integer_too_large);
145 t = Context.UnsignedLongLongTy;
146 assert(Context.getIntegerBitwidth(t, Tok.getLocation()) ==
147 ResultVal.getBitWidth() && "long long is not intmax_t?");
Steve Naroff09ef4742007-03-09 23:16:33 +0000148 } else {
Chris Lattner67ca9252007-05-21 01:08:44 +0000149 // If this value fits into a ULL, try to figure out what else it fits into
150 // according to the rules of C99 6.4.4.1p5.
151
152 // Octal, Hexadecimal, and integers with a U suffix are allowed to
153 // be an unsigned int.
154 bool AllowUnsigned = Literal.isUnsigned || Literal.getRadix() != 10;
155
156 // Check from smallest to largest, picking the smallest type we can.
157 if (!Literal.isLong) { // Are int/unsigned possibilities?
158 unsigned IntSize = Context.Target.getIntWidth(Tok.getLocation());
159 // Does it fit in a unsigned int?
160 if (ResultVal.isIntN(IntSize)) {
161 // Does it fit in a signed int?
162 if (!Literal.isUnsigned && ResultVal[IntSize-1] == 0)
163 t = Context.IntTy;
164 else if (AllowUnsigned)
165 t = Context.UnsignedIntTy;
166 }
167
168 if (!t.isNull())
169 ResultVal.trunc(IntSize);
170 }
171
172 // Are long/unsigned long possibilities?
173 if (t.isNull() && !Literal.isLongLong) {
174 unsigned LongSize = Context.Target.getLongWidth(Tok.getLocation());
175
176 // Does it fit in a unsigned long?
177 if (ResultVal.isIntN(LongSize)) {
178 // Does it fit in a signed long?
179 if (!Literal.isUnsigned && ResultVal[LongSize-1] == 0)
180 t = Context.LongTy;
181 else if (AllowUnsigned)
182 t = Context.UnsignedLongTy;
183 }
184 if (!t.isNull())
185 ResultVal.trunc(LongSize);
186 }
187
188 // Finally, check long long if needed.
189 if (t.isNull()) {
190 unsigned LongLongSize =
191 Context.Target.getLongLongWidth(Tok.getLocation());
192
193 // Does it fit in a unsigned long long?
194 if (ResultVal.isIntN(LongLongSize)) {
195 // Does it fit in a signed long long?
196 if (!Literal.isUnsigned && ResultVal[LongLongSize-1] == 0)
197 t = Context.LongLongTy;
198 else if (AllowUnsigned)
199 t = Context.UnsignedLongLongTy;
200 }
201 }
202
203 // If we still couldn't decide a type, we probably have something that
204 // does not fit in a signed long long, but has no U suffix.
205 if (t.isNull()) {
206 Diag(Tok.getLocation(), diag::warn_integer_too_large_for_signed);
207 t = Context.UnsignedLongLongTy;
208 }
Steve Naroff09ef4742007-03-09 23:16:33 +0000209 }
Chris Lattner67ca9252007-05-21 01:08:44 +0000210
211 return new IntegerLiteral(ResultVal, t, Tok.getLocation());
Steve Naroff09ef4742007-03-09 23:16:33 +0000212 } else if (Literal.isFloatingLiteral()) {
Steve Narofff1e53692007-03-23 22:27:02 +0000213 // FIXME: fill in the value and compute the real type...
Steve Naroff509fe022007-05-17 01:16:00 +0000214 return new FloatingLiteral(7.7, Context.FloatTy, Tok.getLocation());
Steve Naroff09ef4742007-03-09 23:16:33 +0000215 }
Steve Narofff2fb89e2007-03-13 20:29:44 +0000216 return ExprResult(true);
Chris Lattnere168f762006-11-10 05:29:30 +0000217}
218
219Action::ExprResult Sema::ParseParenExpr(SourceLocation L, SourceLocation R,
220 ExprTy *Val) {
Steve Naroffae4143e2007-04-26 20:39:23 +0000221 Expr *e = (Expr *)Val;
222 assert((e != 0) && "ParseParenExpr() missing expr");
Steve Naroff53f07dc2007-05-17 21:49:33 +0000223 return new ParenExpr(L, R, e);
Chris Lattnere168f762006-11-10 05:29:30 +0000224}
225
Steve Naroffa78fe7e2007-05-16 19:47:19 +0000226/// The UsualUnaryConversion() function is *not* called by this routine.
227/// See C99 6.3.2.1p[2-4] for more details.
Steve Naroff043d45d2007-05-15 02:32:35 +0000228QualType Sema::CheckSizeOfAlignOfOperand(QualType exprType,
229 SourceLocation OpLoc, bool isSizeof) {
230 // C99 6.5.3.4p1:
231 if (isa<FunctionType>(exprType) && isSizeof)
232 // alignof(function) is allowed.
233 Diag(OpLoc, diag::ext_sizeof_function_type);
234 else if (exprType->isVoidType())
235 Diag(OpLoc, diag::ext_sizeof_void_type, isSizeof ? "sizeof" : "__alignof");
236 else if (exprType->isIncompleteType()) {
Steve Naroff043d45d2007-05-15 02:32:35 +0000237 Diag(OpLoc, isSizeof ? diag::err_sizeof_incomplete_type :
Chris Lattner3dc3d772007-05-16 18:07:12 +0000238 diag::err_alignof_incomplete_type,
239 exprType.getAsString());
Steve Naroff043d45d2007-05-15 02:32:35 +0000240 return QualType(); // error
241 }
242 // C99 6.5.3.4p4: the type (an unsigned integer type) is size_t.
243 return Context.getSizeType();
244}
245
Chris Lattnere168f762006-11-10 05:29:30 +0000246Action::ExprResult Sema::
247ParseSizeOfAlignOfTypeExpr(SourceLocation OpLoc, bool isSizeof,
Steve Naroff509fe022007-05-17 01:16:00 +0000248 SourceLocation LPLoc, TypeTy *Ty,
249 SourceLocation RPLoc) {
Chris Lattner0d8b1a12006-11-20 04:34:45 +0000250 // If error parsing type, ignore.
251 if (Ty == 0) return true;
Chris Lattner6531c102007-01-23 22:29:49 +0000252
253 // Verify that this is a valid expression.
Steve Naroffe5aa9be2007-04-05 22:36:20 +0000254 QualType ArgTy = QualType::getFromOpaquePtr(Ty);
Chris Lattner6531c102007-01-23 22:29:49 +0000255
Steve Naroff043d45d2007-05-15 02:32:35 +0000256 QualType resultType = CheckSizeOfAlignOfOperand(ArgTy, OpLoc, isSizeof);
257
258 if (resultType.isNull())
259 return true;
Steve Naroff509fe022007-05-17 01:16:00 +0000260 return new SizeOfAlignOfTypeExpr(isSizeof, ArgTy, resultType, OpLoc, RPLoc);
Chris Lattnere168f762006-11-10 05:29:30 +0000261}
262
263
264Action::ExprResult Sema::ParsePostfixUnaryOp(SourceLocation OpLoc,
265 tok::TokenKind Kind,
266 ExprTy *Input) {
267 UnaryOperator::Opcode Opc;
268 switch (Kind) {
269 default: assert(0 && "Unknown unary op!");
270 case tok::plusplus: Opc = UnaryOperator::PostInc; break;
271 case tok::minusminus: Opc = UnaryOperator::PostDec; break;
272 }
Steve Naroff71ce2e02007-05-18 22:53:50 +0000273 QualType result = CheckIncrementDecrementOperand((Expr *)Input, OpLoc);
Steve Naroff35d85152007-05-07 00:24:15 +0000274 if (result.isNull())
275 return true;
Steve Naroff509fe022007-05-17 01:16:00 +0000276 return new UnaryOperator((Expr *)Input, Opc, result, OpLoc);
Chris Lattnere168f762006-11-10 05:29:30 +0000277}
278
279Action::ExprResult Sema::
280ParseArraySubscriptExpr(ExprTy *Base, SourceLocation LLoc,
281 ExprTy *Idx, SourceLocation RLoc) {
Steve Naroffe5aa9be2007-04-05 22:36:20 +0000282 QualType t1 = ((Expr *)Base)->getType();
283 QualType t2 = ((Expr *)Idx)->getType();
Steve Narofff1e53692007-03-23 22:27:02 +0000284
285 assert(!t1.isNull() && "no type for array base expression");
Steve Naroffc1aadb12007-03-28 21:49:40 +0000286 assert(!t2.isNull() && "no type for array index expression");
Steve Narofff1e53692007-03-23 22:27:02 +0000287
Steve Naroffe5aa9be2007-04-05 22:36:20 +0000288 QualType canonT1 = t1.getCanonicalType();
289 QualType canonT2 = t2.getCanonicalType();
Steve Naroffd50c88e2007-04-05 21:15:20 +0000290
Steve Naroffc1aadb12007-03-28 21:49:40 +0000291 // C99 6.5.2.1p2: the expression e1[e2] is by definition precisely equivalent
292 // to the expression *((e1)+(e2)). This means the array "Base" may actually be
Steve Narofff1e53692007-03-23 22:27:02 +0000293 // in the subscript position. As a result, we need to derive the array base
294 // and index from the expression types.
295
Steve Naroffe5aa9be2007-04-05 22:36:20 +0000296 QualType baseType, indexType;
Steve Naroffd50c88e2007-04-05 21:15:20 +0000297 if (isa<ArrayType>(canonT1) || isa<PointerType>(canonT1)) {
298 baseType = canonT1;
299 indexType = canonT2;
300 } else if (isa<ArrayType>(canonT2) || isa<PointerType>(canonT2)) { // uncommon
301 baseType = canonT2;
302 indexType = canonT1;
Steve Narofff1e53692007-03-23 22:27:02 +0000303 } else
304 return Diag(LLoc, diag::err_typecheck_subscript_value);
305
Steve Naroffc1aadb12007-03-28 21:49:40 +0000306 // C99 6.5.2.1p1
Steve Naroff1926c832007-04-24 00:23:05 +0000307 if (!indexType->isIntegerType())
Steve Narofff1e53692007-03-23 22:27:02 +0000308 return Diag(LLoc, diag::err_typecheck_subscript);
Steve Naroffc1aadb12007-03-28 21:49:40 +0000309
Steve Naroff95af0132007-03-30 23:47:58 +0000310 // FIXME: need to deal with const...
Steve Naroffe5aa9be2007-04-05 22:36:20 +0000311 QualType resultType;
Steve Naroffc1aadb12007-03-28 21:49:40 +0000312 if (ArrayType *ary = dyn_cast<ArrayType>(baseType)) {
313 resultType = ary->getElementType();
314 } else if (PointerType *ary = dyn_cast<PointerType>(baseType)) {
315 resultType = ary->getPointeeType();
316 // in practice, the following check catches trying to index a pointer
317 // to a function (e.g. void (*)(int)). Functions are not objects in c99.
Steve Naroffbc2f0992007-03-30 20:09:34 +0000318 if (!resultType->isObjectType())
Chris Lattnerc04bd6a2007-05-16 18:09:54 +0000319 return Diag(LLoc, diag::err_typecheck_subscript_not_object,
320 baseType.getAsString());
Steve Naroffc1aadb12007-03-28 21:49:40 +0000321 }
Steve Naroff509fe022007-05-17 01:16:00 +0000322 return new ArraySubscriptExpr((Expr*)Base, (Expr*)Idx, resultType, RLoc);
Chris Lattnere168f762006-11-10 05:29:30 +0000323}
324
325Action::ExprResult Sema::
326ParseMemberReferenceExpr(ExprTy *Base, SourceLocation OpLoc,
327 tok::TokenKind OpKind, SourceLocation MemberLoc,
328 IdentifierInfo &Member) {
Steve Naroffe5aa9be2007-04-05 22:36:20 +0000329 QualType qualifiedType = ((Expr *)Base)->getType();
Steve Naroffca8f7122007-04-01 01:41:35 +0000330
331 assert(!qualifiedType.isNull() && "no type for member expression");
332
Steve Naroffe5aa9be2007-04-05 22:36:20 +0000333 QualType canonType = qualifiedType.getCanonicalType();
Steve Narofff1e53692007-03-23 22:27:02 +0000334
335 if (OpKind == tok::arrow) {
Steve Naroffca8f7122007-04-01 01:41:35 +0000336 if (PointerType *PT = dyn_cast<PointerType>(canonType)) {
337 qualifiedType = PT->getPointeeType();
Steve Naroffd50c88e2007-04-05 21:15:20 +0000338 canonType = qualifiedType.getCanonicalType();
Steve Naroffca8f7122007-04-01 01:41:35 +0000339 } else
Steve Narofff1e53692007-03-23 22:27:02 +0000340 return Diag(OpLoc, diag::err_typecheck_member_reference_arrow);
341 }
Steve Naroff92e30f82007-04-02 22:35:25 +0000342 if (!isa<RecordType>(canonType))
343 return Diag(OpLoc, diag::err_typecheck_member_reference_structUnion);
344
345 // get the struct/union definition from the type.
346 RecordDecl *RD = cast<RecordType>(canonType)->getDecl();
Steve Naroffcc321422007-03-26 23:09:51 +0000347
Steve Naroff92e30f82007-04-02 22:35:25 +0000348 if (canonType->isIncompleteType())
349 return Diag(OpLoc, diag::err_typecheck_incomplete_tag, RD->getName());
Steve Naroffcc321422007-03-26 23:09:51 +0000350
Steve Naroff92e30f82007-04-02 22:35:25 +0000351 FieldDecl *MemberDecl = RD->getMember(&Member);
352 if (!MemberDecl)
353 return Diag(OpLoc, diag::err_typecheck_no_member, Member.getName());
354
355 return new MemberExpr((Expr*)Base, OpKind == tok::arrow, MemberDecl);
Chris Lattnere168f762006-11-10 05:29:30 +0000356}
357
358/// ParseCallExpr - Handle a call to Fn with the specified array of arguments.
359/// This provides the location of the left/right parens and a list of comma
360/// locations.
361Action::ExprResult Sema::
362ParseCallExpr(ExprTy *Fn, SourceLocation LParenLoc,
Steve Naroffb8c289d2007-05-08 22:18:00 +0000363 ExprTy **Args, unsigned NumArgsInCall,
Chris Lattnere168f762006-11-10 05:29:30 +0000364 SourceLocation *CommaLocs, SourceLocation RParenLoc) {
Steve Naroffae4143e2007-04-26 20:39:23 +0000365 QualType qType = ((Expr *)Fn)->getType();
366
367 assert(!qType.isNull() && "no type for function call expression");
368
Steve Naroff17f76e02007-05-03 21:03:48 +0000369 const FunctionType *funcT = dyn_cast<FunctionType>(qType.getCanonicalType());
Steve Naroffae4143e2007-04-26 20:39:23 +0000370
Steve Naroff17f76e02007-05-03 21:03:48 +0000371 assert(funcT && "ParseCallExpr(): not a function type");
Steve Naroffb8c289d2007-05-08 22:18:00 +0000372
Steve Naroff17f76e02007-05-03 21:03:48 +0000373 // If a prototype isn't declared, the parser implicitly defines a func decl
374 QualType resultType = funcT->getResultType();
375
376 if (const FunctionTypeProto *proto = dyn_cast<FunctionTypeProto>(funcT)) {
377 // C99 6.5.2.2p7 - the arguments are implicitly converted, as if by
378 // assignment, to the types of the corresponding parameter, ...
379
380 unsigned NumArgsInProto = proto->getNumArgs();
Steve Naroffb8c289d2007-05-08 22:18:00 +0000381 unsigned NumArgsToCheck = NumArgsInCall;
Steve Naroff17f76e02007-05-03 21:03:48 +0000382
Steve Naroffb8c289d2007-05-08 22:18:00 +0000383 if (NumArgsInCall < NumArgsInProto)
Steve Naroff1f4d7272007-05-11 04:00:31 +0000384 Diag(LParenLoc, diag::err_typecheck_call_too_few_args);
Steve Naroffb8c289d2007-05-08 22:18:00 +0000385 else if (NumArgsInCall > NumArgsInProto) {
386 if (!proto->isVariadic())
Steve Naroff1f4d7272007-05-11 04:00:31 +0000387 Diag(LParenLoc, diag::err_typecheck_call_too_many_args);
Steve Naroffb8c289d2007-05-08 22:18:00 +0000388 NumArgsToCheck = NumArgsInProto;
Steve Naroff17f76e02007-05-03 21:03:48 +0000389 }
390 // Continue to check argument types (even if we have too few/many args).
Steve Naroffb8c289d2007-05-08 22:18:00 +0000391 for (unsigned i = 0; i < NumArgsToCheck; i++) {
Steve Naroff17f76e02007-05-03 21:03:48 +0000392 QualType lhsType = proto->getArgType(i);
393 QualType rhsType = ((Expr **)Args)[i]->getType();
394
395 if (lhsType == rhsType) // common case, fast path...
396 continue;
397 AssignmentConversionResult result;
Steve Naroff218bc2b2007-05-04 21:54:46 +0000398 UsualAssignmentConversions(lhsType, rhsType, result);
Steve Naroff17f76e02007-05-03 21:03:48 +0000399
400 SourceLocation l = (i == 0) ? LParenLoc : CommaLocs[i-1];
401
402 // decode the result (notice that AST's are still created for extensions).
403 // FIXME: consider fancier error diagnostics (since this is quite common).
404 // #1: emit the actual prototype arg...requires adding source loc info.
405 // #2: pass Diag the offending argument type...requires hacking Diag.
406 switch (result) {
407 case Compatible:
408 break;
409 case PointerFromInt:
Steve Naroff218bc2b2007-05-04 21:54:46 +0000410 // check for null pointer constant (C99 6.3.2.3p3)
411 if (!((Expr **)Args)[i]->isNullPointerConstant())
412 Diag(l, diag::ext_typecheck_passing_pointer_from_int, utostr(i+1));
Steve Naroff17f76e02007-05-03 21:03:48 +0000413 break;
414 case IntFromPointer:
415 Diag(l, diag::ext_typecheck_passing_int_from_pointer, utostr(i+1));
416 break;
417 case IncompatiblePointer:
418 Diag(l, diag::ext_typecheck_passing_incompatible_pointer, utostr(i+1));
419 break;
Steve Naroff1f4d7272007-05-11 04:00:31 +0000420 case CompatiblePointerDiscardsQualifiers:
421 Diag(l, diag::ext_typecheck_passing_discards_qualifiers, utostr(i+1));
422 break;
Steve Naroff17f76e02007-05-03 21:03:48 +0000423 case Incompatible:
424 return Diag(l, diag::err_typecheck_passing_incompatible, utostr(i+1));
425 }
426 }
Steve Naroffb8c289d2007-05-08 22:18:00 +0000427 // Even if the types checked, bail if we had the wrong number of arguments.
428 if ((NumArgsInCall != NumArgsInProto) && !proto->isVariadic())
429 return true;
Steve Naroffae4143e2007-04-26 20:39:23 +0000430 }
Steve Naroff509fe022007-05-17 01:16:00 +0000431 return new CallExpr((Expr*)Fn, (Expr**)Args, NumArgsInCall, resultType,
432 RParenLoc);
Chris Lattnere168f762006-11-10 05:29:30 +0000433}
434
435Action::ExprResult Sema::
436ParseCastExpr(SourceLocation LParenLoc, TypeTy *Ty,
437 SourceLocation RParenLoc, ExprTy *Op) {
Chris Lattner0d8b1a12006-11-20 04:34:45 +0000438 // If error parsing type, ignore.
Steve Naroffae4143e2007-04-26 20:39:23 +0000439 assert((Ty != 0) && "ParseCastExpr(): missing type");
Steve Naroff509fe022007-05-17 01:16:00 +0000440 return new CastExpr(QualType::getFromOpaquePtr(Ty), (Expr*)Op, LParenLoc);
Chris Lattnere168f762006-11-10 05:29:30 +0000441}
442
Steve Narofff8a28c52007-05-15 20:29:32 +0000443inline QualType Sema::CheckConditionalOperands( // C99 6.5.15
444 Expr *Cond, Expr *LHS, Expr *RHS, SourceLocation questionLoc) {
Steve Naroffa78fe7e2007-05-16 19:47:19 +0000445 QualType cond = Cond->getType();
446 QualType lhs = LHS->getType();
447 QualType rhs = RHS->getType();
448
Steve Narofff8a28c52007-05-15 20:29:32 +0000449 assert(!cond.isNull() && "ParseConditionalOp(): no conditional type");
450 assert(!lhs.isNull() && "ParseConditionalOp(): no lhs type");
451 assert(!rhs.isNull() && "ParseConditionalOp(): no rhs type");
452
Steve Naroffa78fe7e2007-05-16 19:47:19 +0000453 cond = UsualUnaryConversion(cond);
454 lhs = UsualUnaryConversion(lhs);
455 rhs = UsualUnaryConversion(rhs);
456
457 // first, check the condition.
458 if (!cond->isScalarType()) { // C99 6.5.15p2
Steve Naroff53f07dc2007-05-17 21:49:33 +0000459 Diag(Cond->getLocStart(), diag::err_typecheck_cond_expect_scalar,
Steve Naroff509fe022007-05-17 01:16:00 +0000460 cond.getAsString());
Steve Naroffa78fe7e2007-05-16 19:47:19 +0000461 return QualType();
462 }
463 // now check the two expressions.
464 if (lhs->isArithmeticType() && rhs->isArithmeticType()) // C99 6.5.15p3,5
465 return UsualArithmeticConversions(lhs, rhs);
466
467 if ((lhs->isStructureType() && rhs->isStructureType()) || // C99 6.5.15p3
468 (lhs->isUnionType() && rhs->isUnionType())) {
469 TagType *lTag = cast<TagType>(lhs.getCanonicalType());
470 TagType *rTag = cast<TagType>(rhs.getCanonicalType());
471
472 if (lTag->getDecl()->getIdentifier() == rTag->getDecl()->getIdentifier())
473 return lhs;
474 else {
475 Diag(questionLoc, diag::err_typecheck_cond_incompatible_operands,
476 lhs.getAsString(), rhs.getAsString());
477 return QualType();
478 }
479 }
Steve Naroff30d1fbc2007-05-20 19:46:53 +0000480 if (lhs->isPointerType() && RHS->isNullPointerConstant()) // C99 6.5.15p3
481 return lhs;
482 if (rhs->isPointerType() && LHS->isNullPointerConstant())
483 return rhs;
484
Steve Naroffa78fe7e2007-05-16 19:47:19 +0000485 if (lhs->isPointerType() && rhs->isPointerType()) { // C99 6.5.15p3,6
486 QualType lhptee, rhptee;
487
488 // get the "pointed to" type
489 lhptee = cast<PointerType>(lhs.getCanonicalType())->getPointeeType();
490 rhptee = cast<PointerType>(rhs.getCanonicalType())->getPointeeType();
491
492 // ignore qualifiers on void (C99 6.5.15p3, clause 6)
493 if (lhptee.getUnqualifiedType()->isVoidType() &&
494 (rhptee->isObjectType() || rhptee->isIncompleteType()))
495 return lhs;
496 if (rhptee.getUnqualifiedType()->isVoidType() &&
497 (lhptee->isObjectType() || lhptee->isIncompleteType()))
498 return rhs;
499
500 // FIXME: C99 6.5.15p6: If both operands are pointers to compatible types
501 // *or* to differently qualified versions of compatible types, the result
502 // type is a pointer to an appropriately qualified version of the
503 // *composite* type.
504 if (!Type::typesAreCompatible(lhptee.getUnqualifiedType(),
505 rhptee.getUnqualifiedType())) {
506 Diag(questionLoc, diag::ext_typecheck_cond_incompatible_pointers,
507 lhs.getAsString(), rhs.getAsString());
508 return lhs; // FIXME: this is an _ext - is this return o.k?
509 }
510 }
511 if (lhs->isVoidType() && rhs->isVoidType()) // C99 6.5.15p3
512 return lhs;
Steve Naroffa78fe7e2007-05-16 19:47:19 +0000513
514 Diag(questionLoc, diag::err_typecheck_cond_incompatible_operands,
515 lhs.getAsString(), rhs.getAsString());
516 return QualType();
Steve Narofff8a28c52007-05-15 20:29:32 +0000517}
518
Chris Lattnere168f762006-11-10 05:29:30 +0000519/// ParseConditionalOp - Parse a ?: operation. Note that 'LHS' may be null
520/// in the case of a the GNU conditional expr extension.
521Action::ExprResult Sema::ParseConditionalOp(SourceLocation QuestionLoc,
522 SourceLocation ColonLoc,
523 ExprTy *Cond, ExprTy *LHS,
524 ExprTy *RHS) {
Steve Narofff8a28c52007-05-15 20:29:32 +0000525 QualType result = CheckConditionalOperands((Expr *)Cond, (Expr *)LHS,
526 (Expr *)RHS, QuestionLoc);
527 if (result.isNull())
528 return true;
529 return new ConditionalOperator((Expr*)Cond, (Expr*)LHS, (Expr*)RHS, result);
Chris Lattnere168f762006-11-10 05:29:30 +0000530}
531
Steve Naroff1926c832007-04-24 00:23:05 +0000532/// UsualUnaryConversion - Performs various conversions that are common to most
533/// operators (C99 6.3). The conversions of array and function types are
534/// sometimes surpressed. For example, the array->pointer conversion doesn't
535/// apply if the array is an argument to the sizeof or address (&) operators.
536/// In these instances, this routine should *not* be called.
537QualType Sema::UsualUnaryConversion(QualType t) {
538 assert(!t.isNull() && "UsualUnaryConversion - missing type");
Steve Naroff4b7ce032007-04-20 22:26:17 +0000539
Steve Naroff1926c832007-04-24 00:23:05 +0000540 if (t->isPromotableIntegerType()) // C99 6.3.1.1p2
541 return Context.IntTy;
Steve Naroffa78fe7e2007-05-16 19:47:19 +0000542 if (t->isFunctionType()) // C99 6.3.2.1p4
543 return Context.getPointerType(t.getCanonicalType());
544 if (const ArrayType *ary = dyn_cast<ArrayType>(t.getCanonicalType()))
545 return Context.getPointerType(ary->getElementType()); // C99 6.3.2.1p3
Steve Naroff1926c832007-04-24 00:23:05 +0000546 return t;
547}
548
549/// UsualArithmeticConversions - Performs various conversions that are common to
550/// binary operators (C99 6.3.1.8). If both operands aren't arithmetic, this
551/// routine returns the first non-arithmetic type found. The client is
552/// responsible for emitting appropriate error diagnostics.
553QualType Sema::UsualArithmeticConversions(QualType t1, QualType t2) {
Steve Naroffb01bbe32007-04-25 01:22:31 +0000554 QualType lhs = UsualUnaryConversion(t1);
555 QualType rhs = UsualUnaryConversion(t2);
Steve Naroff1926c832007-04-24 00:23:05 +0000556
557 // if either operand is not of arithmetic type, no conversion is possible.
Steve Naroffb01bbe32007-04-25 01:22:31 +0000558 if (!lhs->isArithmeticType())
559 return lhs;
Steve Narofff633d092007-04-25 19:01:39 +0000560 if (!rhs->isArithmeticType())
Steve Naroffb01bbe32007-04-25 01:22:31 +0000561 return rhs;
Steve Naroff1926c832007-04-24 00:23:05 +0000562
Steve Narofff633d092007-04-25 19:01:39 +0000563 // if both arithmetic types are identical, no conversion is needed.
Steve Naroffb01bbe32007-04-25 01:22:31 +0000564 if (lhs == rhs)
565 return lhs;
Steve Naroff1926c832007-04-24 00:23:05 +0000566
Steve Naroffb01bbe32007-04-25 01:22:31 +0000567 // at this point, we have two different arithmetic types.
568
569 // Handle complex types first (C99 6.3.1.8p1).
570 if (lhs->isComplexType() || rhs->isComplexType()) {
571 // if we have an integer operand, the result is the complex type.
572 if (rhs->isIntegerType())
573 return lhs;
574 if (lhs->isIntegerType())
575 return rhs;
576
Steve Naroffe4718892007-04-27 18:30:00 +0000577 return Context.maxComplexType(lhs, rhs);
Steve Naroffbf223ba2007-04-24 20:56:26 +0000578 }
Steve Naroffb01bbe32007-04-25 01:22:31 +0000579 // Now handle "real" floating types (i.e. float, double, long double).
580 if (lhs->isRealFloatingType() || rhs->isRealFloatingType()) {
581 // if we have an integer operand, the result is the real floating type.
582 if (rhs->isIntegerType())
583 return lhs;
584 if (lhs->isIntegerType())
585 return rhs;
586
587 // we have two real floating types, float/complex combos were handled above.
Steve Naroffe4718892007-04-27 18:30:00 +0000588 return Context.maxFloatingType(lhs, rhs);
Steve Naroffb01bbe32007-04-25 01:22:31 +0000589 }
Steve Naroffe4718892007-04-27 18:30:00 +0000590 return Context.maxIntegerType(lhs, rhs);
Steve Narofff1e53692007-03-23 22:27:02 +0000591}
592
Steve Naroff3f597292007-05-11 22:18:03 +0000593// CheckPointerTypesForAssignment - This is a very tricky routine (despite
594// being closely modeled after the C99 spec:-). The odd characteristic of this
595// routine is it effectively iqnores the qualifiers on the top level pointee.
596// This circumvents the usual type rules specified in 6.2.7p1 & 6.7.5.[1-3].
597// FIXME: add a couple examples in this comment.
598QualType Sema::CheckPointerTypesForAssignment(QualType lhsType,
599 QualType rhsType,
600 AssignmentConversionResult &r) {
601 QualType lhptee, rhptee;
Steve Naroff1f4d7272007-05-11 04:00:31 +0000602
603 // get the "pointed to" type (ignoring qualifiers at the top level)
Steve Naroff3f597292007-05-11 22:18:03 +0000604 lhptee = cast<PointerType>(lhsType.getCanonicalType())->getPointeeType();
605 rhptee = cast<PointerType>(rhsType.getCanonicalType())->getPointeeType();
Steve Naroff1f4d7272007-05-11 04:00:31 +0000606
607 // make sure we operate on the canonical type
Steve Naroff3f597292007-05-11 22:18:03 +0000608 lhptee = lhptee.getCanonicalType();
609 rhptee = rhptee.getCanonicalType();
Steve Naroff1f4d7272007-05-11 04:00:31 +0000610
Steve Naroff3f597292007-05-11 22:18:03 +0000611 // C99 6.5.16.1p1: This following citation is common to constraints
612 // 3 & 4 (below). ...and the type *pointed to* by the left has all the
613 // qualifiers of the type *pointed to* by the right;
614 if ((lhptee.getQualifiers() & rhptee.getQualifiers()) !=
615 rhptee.getQualifiers())
616 r = CompatiblePointerDiscardsQualifiers;
617
618 // C99 6.5.16.1p1 (constraint 4): If one operand is a pointer to an object or
619 // incomplete type and the other is a pointer to a qualified or unqualified
620 // version of void...
621 if (lhptee.getUnqualifiedType()->isVoidType() &&
622 (rhptee->isObjectType() || rhptee->isIncompleteType()))
623 ;
624 else if (rhptee.getUnqualifiedType()->isVoidType() &&
625 (lhptee->isObjectType() || lhptee->isIncompleteType()))
626 ;
627 // C99 6.5.16.1p1 (constraint 3): both operands are pointers to qualified or
628 // unqualified versions of compatible types, ...
629 else if (!Type::typesAreCompatible(lhptee.getUnqualifiedType(),
630 rhptee.getUnqualifiedType()))
631 r = IncompatiblePointer; // this "trumps" PointerAssignDiscardsQualifiers
632 return rhsType;
Steve Naroff1f4d7272007-05-11 04:00:31 +0000633}
634
Steve Naroff17f76e02007-05-03 21:03:48 +0000635/// UsualAssignmentConversions (C99 6.5.16) - This routine currently
636/// has code to accommodate several GCC extensions when type checking
637/// pointers. Here are some objectionable examples that GCC considers warnings:
638///
639/// int a, *pint;
640/// short *pshort;
641/// struct foo *pfoo;
642///
643/// pint = pshort; // warning: assignment from incompatible pointer type
644/// a = pint; // warning: assignment makes integer from pointer without a cast
645/// pint = a; // warning: assignment makes pointer from integer without a cast
646/// pint = pfoo; // warning: assignment from incompatible pointer type
647///
648/// As a result, the code for dealing with pointers is more complex than the
649/// C99 spec dictates.
650/// Note: the warning above turn into errors when -pedantic-errors is enabled.
651///
Steve Naroff218bc2b2007-05-04 21:54:46 +0000652QualType Sema::UsualAssignmentConversions(QualType lhsType, QualType rhsType,
Steve Naroff17f76e02007-05-03 21:03:48 +0000653 AssignmentConversionResult &r) {
Steve Naroffb891de32007-05-02 23:51:10 +0000654 // this check seems unnatural, however it necessary to insure the proper
655 // conversion of functions/arrays. If the conversion where done for all
656 // DeclExpr's (created by ParseIdentifierExpr), it would mess up the
657 // unary expressions that surpress this implicit conversion (&, sizeof).
658 if (rhsType->isFunctionType() || rhsType->isArrayType())
659 rhsType = UsualUnaryConversion(rhsType);
660
Steve Naroff17f76e02007-05-03 21:03:48 +0000661 r = Compatible;
Steve Naroff9eb24652007-05-02 21:58:15 +0000662 if (lhsType->isArithmeticType() && rhsType->isArithmeticType())
663 return lhsType;
664 else if (lhsType->isPointerType()) {
665 if (rhsType->isIntegerType()) {
Steve Naroff218bc2b2007-05-04 21:54:46 +0000666 r = PointerFromInt;
Steve Naroffb891de32007-05-02 23:51:10 +0000667 return rhsType;
Steve Naroff9eb24652007-05-02 21:58:15 +0000668 }
Steve Naroff3f597292007-05-11 22:18:03 +0000669 if (rhsType->isPointerType())
670 return CheckPointerTypesForAssignment(lhsType, rhsType, r);
Steve Naroff9eb24652007-05-02 21:58:15 +0000671 } else if (rhsType->isPointerType()) {
672 if (lhsType->isIntegerType()) {
673 // C99 6.5.16.1p1: the left operand is _Bool and the right is a pointer.
674 if (lhsType != Context.BoolTy)
Steve Naroff17f76e02007-05-03 21:03:48 +0000675 r = IntFromPointer;
Steve Naroffb891de32007-05-02 23:51:10 +0000676 return rhsType;
Steve Naroff9eb24652007-05-02 21:58:15 +0000677 }
Steve Naroff3f597292007-05-11 22:18:03 +0000678 if (lhsType->isPointerType())
679 return CheckPointerTypesForAssignment(lhsType, rhsType, r);
Steve Naroff1f4d7272007-05-11 04:00:31 +0000680 } else if (isa<TagType>(lhsType) && isa<TagType>(rhsType)) {
681 if (Type::tagTypesAreCompatible(lhsType, rhsType))
Steve Naroffb891de32007-05-02 23:51:10 +0000682 return rhsType;
Steve Naroff1f4d7272007-05-11 04:00:31 +0000683 }
Steve Naroff17f76e02007-05-03 21:03:48 +0000684 r = Incompatible;
685 return QualType();
Steve Naroff9eb24652007-05-02 21:58:15 +0000686}
687
Steve Naroff218bc2b2007-05-04 21:54:46 +0000688inline QualType Sema::CheckMultiplyDivideOperands(
689 Expr *lex, Expr *rex, SourceLocation loc)
Steve Naroff5c10d4b2007-04-20 23:42:24 +0000690{
Steve Naroff1926c832007-04-24 00:23:05 +0000691 QualType resType = UsualArithmeticConversions(lex->getType(), rex->getType());
Steve Naroff5c10d4b2007-04-20 23:42:24 +0000692
Steve Naroff218bc2b2007-05-04 21:54:46 +0000693 if (resType->isArithmeticType())
694 return resType;
Steve Naroffe845e272007-05-18 01:06:45 +0000695 Diag(loc, diag::err_typecheck_invalid_operands,
Chris Lattner84e160a2007-05-19 07:03:17 +0000696 lex->getType().getAsString(), rex->getType().getAsString(),
697 lex->getSourceRange(), rex->getSourceRange());
Steve Naroff218bc2b2007-05-04 21:54:46 +0000698 return QualType();
Steve Naroff26c8ea52007-03-21 21:08:52 +0000699}
700
Steve Naroff218bc2b2007-05-04 21:54:46 +0000701inline QualType Sema::CheckRemainderOperands(
702 Expr *lex, Expr *rex, SourceLocation loc)
703{
704 QualType resType = UsualArithmeticConversions(lex->getType(), rex->getType());
705
706 if (resType->isIntegerType())
707 return resType;
Steve Naroffe845e272007-05-18 01:06:45 +0000708 Diag(loc, diag::err_typecheck_invalid_operands,
Chris Lattner84e160a2007-05-19 07:03:17 +0000709 lex->getType().getAsString(), rex->getType().getAsString(),
710 lex->getSourceRange(), rex->getSourceRange());
Steve Naroff218bc2b2007-05-04 21:54:46 +0000711 return QualType();
712}
713
714inline QualType Sema::CheckAdditionOperands( // C99 6.5.6
715 Expr *lex, Expr *rex, SourceLocation loc)
Steve Naroff1926c832007-04-24 00:23:05 +0000716{
Steve Naroffe4718892007-04-27 18:30:00 +0000717 QualType lhsType = lex->getType(), rhsType = rex->getType();
718 QualType resType = UsualArithmeticConversions(lhsType, rhsType);
719
720 // handle the common case first (both operands are arithmetic).
721 if (resType->isArithmeticType())
Steve Naroff218bc2b2007-05-04 21:54:46 +0000722 return resType;
723
724 if ((lhsType->isPointerType() && rhsType->isIntegerType()) ||
725 (lhsType->isIntegerType() && rhsType->isPointerType()))
726 return resType;
Steve Naroffe845e272007-05-18 01:06:45 +0000727 Diag(loc, diag::err_typecheck_invalid_operands,
Chris Lattner84e160a2007-05-19 07:03:17 +0000728 lhsType.getAsString(), rhsType.getAsString(),
729 lex->getSourceRange(), rex->getSourceRange());
Steve Naroff218bc2b2007-05-04 21:54:46 +0000730 return QualType();
Steve Naroff26c8ea52007-03-21 21:08:52 +0000731}
732
Steve Naroff218bc2b2007-05-04 21:54:46 +0000733inline QualType Sema::CheckSubtractionOperands( // C99 6.5.6
734 Expr *lex, Expr *rex, SourceLocation loc)
735{
736 QualType lhsType = lex->getType(), rhsType = rex->getType();
737 QualType resType = UsualArithmeticConversions(lhsType, rhsType);
738
739 // handle the common case first (both operands are arithmetic).
740 if (resType->isArithmeticType())
741 return resType;
742 if ((lhsType->isPointerType() && rhsType->isIntegerType()) ||
743 (lhsType->isPointerType() && rhsType->isPointerType()))
744 return resType;
Steve Naroffe845e272007-05-18 01:06:45 +0000745 Diag(loc, diag::err_typecheck_invalid_operands,
Chris Lattner84e160a2007-05-19 07:03:17 +0000746 lhsType.getAsString(), rhsType.getAsString(),
747 lex->getSourceRange(), rex->getSourceRange());
Steve Naroff218bc2b2007-05-04 21:54:46 +0000748 return QualType();
749}
750
751inline QualType Sema::CheckShiftOperands( // C99 6.5.7
752 Expr *lex, Expr *rex, SourceLocation loc)
Steve Naroff1926c832007-04-24 00:23:05 +0000753{
754 QualType resType = UsualArithmeticConversions(lex->getType(), rex->getType());
755
Steve Naroff218bc2b2007-05-04 21:54:46 +0000756 if (resType->isIntegerType())
757 return resType;
Steve Naroffe845e272007-05-18 01:06:45 +0000758 Diag(loc, diag::err_typecheck_invalid_operands,
Chris Lattner84e160a2007-05-19 07:03:17 +0000759 lex->getType().getAsString(), rex->getType().getAsString(),
760 lex->getSourceRange(), rex->getSourceRange());
Steve Naroff218bc2b2007-05-04 21:54:46 +0000761 return QualType();
Steve Naroff26c8ea52007-03-21 21:08:52 +0000762}
763
Steve Naroff218bc2b2007-05-04 21:54:46 +0000764inline QualType Sema::CheckRelationalOperands( // C99 6.5.8
765 Expr *lex, Expr *rex, SourceLocation loc)
Steve Naroff1926c832007-04-24 00:23:05 +0000766{
767 QualType lType = lex->getType(), rType = rex->getType();
768
769 if (lType->isRealType() && rType->isRealType())
Steve Naroff218bc2b2007-05-04 21:54:46 +0000770 return Context.IntTy;
Steve Naroffe4718892007-04-27 18:30:00 +0000771
772 if (lType->isPointerType() && rType->isPointerType())
Steve Naroff218bc2b2007-05-04 21:54:46 +0000773 return Context.IntTy;
Steve Naroffe4718892007-04-27 18:30:00 +0000774
Steve Naroff043d45d2007-05-15 02:32:35 +0000775 if (lType->isIntegerType() || rType->isIntegerType()) { // GCC extension.
Steve Naroff218bc2b2007-05-04 21:54:46 +0000776 Diag(loc, diag::ext_typecheck_comparison_of_pointer_integer);
Steve Naroff043d45d2007-05-15 02:32:35 +0000777 return Context.IntTy;
778 }
Steve Naroffe845e272007-05-18 01:06:45 +0000779 Diag(loc, diag::err_typecheck_invalid_operands,
Chris Lattner84e160a2007-05-19 07:03:17 +0000780 lType.getAsString(), rType.getAsString(),
781 lex->getSourceRange(), rex->getSourceRange());
Steve Naroff218bc2b2007-05-04 21:54:46 +0000782 return QualType();
Steve Naroff26c8ea52007-03-21 21:08:52 +0000783}
784
Steve Naroff218bc2b2007-05-04 21:54:46 +0000785inline QualType Sema::CheckEqualityOperands( // C99 6.5.9
786 Expr *lex, Expr *rex, SourceLocation loc)
Steve Naroff1926c832007-04-24 00:23:05 +0000787{
788 QualType lType = lex->getType(), rType = rex->getType();
789
790 if (lType->isArithmeticType() && rType->isArithmeticType())
Steve Naroff218bc2b2007-05-04 21:54:46 +0000791 return Context.IntTy;
Steve Naroffe4718892007-04-27 18:30:00 +0000792 if (lType->isPointerType() && rType->isPointerType())
Steve Naroff218bc2b2007-05-04 21:54:46 +0000793 return Context.IntTy;
794
Steve Naroff043d45d2007-05-15 02:32:35 +0000795 if (lType->isIntegerType() || rType->isIntegerType()) { // GCC extension.
Steve Naroff218bc2b2007-05-04 21:54:46 +0000796 Diag(loc, diag::ext_typecheck_comparison_of_pointer_integer);
Steve Naroff043d45d2007-05-15 02:32:35 +0000797 return Context.IntTy;
798 }
Steve Naroffe845e272007-05-18 01:06:45 +0000799 Diag(loc, diag::err_typecheck_invalid_operands,
Chris Lattner84e160a2007-05-19 07:03:17 +0000800 lType.getAsString(), rType.getAsString(),
801 lex->getSourceRange(), rex->getSourceRange());
Steve Naroff218bc2b2007-05-04 21:54:46 +0000802 return QualType();
Steve Naroff26c8ea52007-03-21 21:08:52 +0000803}
804
Steve Naroff218bc2b2007-05-04 21:54:46 +0000805inline QualType Sema::CheckBitwiseOperands(
806 Expr *lex, Expr *rex, SourceLocation loc)
Steve Naroff1926c832007-04-24 00:23:05 +0000807{
808 QualType resType = UsualArithmeticConversions(lex->getType(), rex->getType());
809
Steve Naroff218bc2b2007-05-04 21:54:46 +0000810 if (resType->isIntegerType())
811 return resType;
Steve Naroffe845e272007-05-18 01:06:45 +0000812 Diag(loc, diag::err_typecheck_invalid_operands,
Chris Lattner84e160a2007-05-19 07:03:17 +0000813 lex->getType().getAsString(), rex->getType().getAsString(),
814 lex->getSourceRange(), rex->getSourceRange());
Steve Naroff218bc2b2007-05-04 21:54:46 +0000815 return QualType();
Steve Naroff26c8ea52007-03-21 21:08:52 +0000816}
817
Steve Naroff218bc2b2007-05-04 21:54:46 +0000818inline QualType Sema::CheckLogicalOperands( // C99 6.5.[13,14]
819 Expr *lex, Expr *rex, SourceLocation loc)
Steve Naroff1926c832007-04-24 00:23:05 +0000820{
Steve Naroffe4718892007-04-27 18:30:00 +0000821 QualType lhsType = UsualUnaryConversion(lex->getType());
822 QualType rhsType = UsualUnaryConversion(rex->getType());
823
Steve Naroff218bc2b2007-05-04 21:54:46 +0000824 if (lhsType->isScalarType() || rhsType->isScalarType())
825 return Context.IntTy;
Steve Naroffe845e272007-05-18 01:06:45 +0000826 Diag(loc, diag::err_typecheck_invalid_operands,
Chris Lattner84e160a2007-05-19 07:03:17 +0000827 lex->getType().getAsString(), rex->getType().getAsString(),
828 lex->getSourceRange(), rex->getSourceRange());
Steve Naroff218bc2b2007-05-04 21:54:46 +0000829 return QualType();
Steve Naroffae4143e2007-04-26 20:39:23 +0000830}
831
Steve Naroff35d85152007-05-07 00:24:15 +0000832inline QualType Sema::CheckAssignmentOperands( // C99 6.5.16.1
833 Expr *lex, Expr *rex, SourceLocation loc, QualType compoundType)
Steve Naroffae4143e2007-04-26 20:39:23 +0000834{
Steve Naroff0af91202007-04-27 21:51:21 +0000835 QualType lhsType = lex->getType();
Steve Naroff35d85152007-05-07 00:24:15 +0000836 QualType rhsType = compoundType.isNull() ? rex->getType() : compoundType;
Steve Naroff1f4d7272007-05-11 04:00:31 +0000837 bool hadError = false;
Steve Naroff218bc2b2007-05-04 21:54:46 +0000838
839 // this check is done first to give a more precise diagnostic.
Steve Naroff1f4d7272007-05-11 04:00:31 +0000840 // isModifiableLvalue() will also check for "const".
Steve Naroff218bc2b2007-05-04 21:54:46 +0000841 if (lhsType.isConstQualified()) {
Steve Naroff71ce2e02007-05-18 22:53:50 +0000842 Diag(loc, diag::err_typecheck_assign_const, lex->getSourceRange());
Steve Naroff1f4d7272007-05-11 04:00:31 +0000843 hadError = true;
844 } else if (!lex->isModifiableLvalue()) { // C99 6.5.16p2
Steve Naroff71ce2e02007-05-18 22:53:50 +0000845 Diag(loc, diag::err_typecheck_assign_non_lvalue, lex->getSourceRange());
Steve Naroff1f4d7272007-05-11 04:00:31 +0000846 return QualType(); // no need to continue checking...
Steve Naroff218bc2b2007-05-04 21:54:46 +0000847 }
848 if (lhsType == rhsType) // common case, fast path...
849 return lhsType;
850
851 AssignmentConversionResult result;
852 QualType resType = UsualAssignmentConversions(lhsType, rhsType, result);
853
854 // decode the result (notice that extensions still return a type).
855 switch (result) {
856 case Compatible:
Steve Naroff1f4d7272007-05-11 04:00:31 +0000857 break;
Steve Naroff218bc2b2007-05-04 21:54:46 +0000858 case Incompatible:
Steve Naroffe845e272007-05-18 01:06:45 +0000859 Diag(loc, diag::err_typecheck_assign_incompatible,
Chris Lattner84e160a2007-05-19 07:03:17 +0000860 lhsType.getAsString(), rhsType.getAsString(),
861 lex->getSourceRange(), rex->getSourceRange());
Steve Naroff1f4d7272007-05-11 04:00:31 +0000862 hadError = true;
863 break;
Steve Naroff218bc2b2007-05-04 21:54:46 +0000864 case PointerFromInt:
865 // check for null pointer constant (C99 6.3.2.3p3)
Steve Naroff35d85152007-05-07 00:24:15 +0000866 if (compoundType.isNull() && !rex->isNullPointerConstant())
Steve Naroff218bc2b2007-05-04 21:54:46 +0000867 Diag(loc, diag::ext_typecheck_assign_pointer_from_int);
Steve Naroff1f4d7272007-05-11 04:00:31 +0000868 break;
Steve Naroff218bc2b2007-05-04 21:54:46 +0000869 case IntFromPointer:
870 Diag(loc, diag::ext_typecheck_assign_int_from_pointer);
Steve Naroff1f4d7272007-05-11 04:00:31 +0000871 break;
Steve Naroff218bc2b2007-05-04 21:54:46 +0000872 case IncompatiblePointer:
873 Diag(loc, diag::ext_typecheck_assign_incompatible_pointer);
Steve Naroff1f4d7272007-05-11 04:00:31 +0000874 break;
875 case CompatiblePointerDiscardsQualifiers:
876 Diag(loc, diag::ext_typecheck_assign_discards_qualifiers);
877 break;
Steve Naroff218bc2b2007-05-04 21:54:46 +0000878 }
Steve Naroff1f4d7272007-05-11 04:00:31 +0000879 return hadError ? QualType() : resType;
Steve Naroffae4143e2007-04-26 20:39:23 +0000880}
881
Steve Naroff218bc2b2007-05-04 21:54:46 +0000882inline QualType Sema::CheckCommaOperands( // C99 6.5.17
Chris Lattner84e160a2007-05-19 07:03:17 +0000883 Expr *lex, Expr *rex, SourceLocation loc) {
Steve Naroff218bc2b2007-05-04 21:54:46 +0000884 return UsualUnaryConversion(rex->getType());
Steve Naroff95af0132007-03-30 23:47:58 +0000885}
886
Steve Naroff71ce2e02007-05-18 22:53:50 +0000887QualType Sema::CheckIncrementDecrementOperand(Expr *op, SourceLocation OpLoc) {
Steve Naroff094046f2007-05-13 03:21:25 +0000888 QualType resType = UsualArithmeticConversions(op->getType(), Context.IntTy);
Steve Naroff35d85152007-05-07 00:24:15 +0000889 assert(!resType.isNull() && "no type for increment/decrement expression");
Steve Naroffd50c88e2007-04-05 21:15:20 +0000890
Steve Naroff46ba1eb2007-04-03 23:13:13 +0000891 // C99 6.5.2.4p1
Steve Naroff35d85152007-05-07 00:24:15 +0000892 if (const PointerType *pt = dyn_cast<PointerType>(resType)) {
893 if (!pt->getPointeeType()->isObjectType()) { // C99 6.5.2.4p2, 6.5.6p2
Steve Naroff71ce2e02007-05-18 22:53:50 +0000894 Diag(OpLoc, diag::err_typecheck_arithmetic_incomplete_type,
895 resType.getAsString(), op->getSourceRange());
Steve Naroff35d85152007-05-07 00:24:15 +0000896 return QualType();
897 }
898 } else if (!resType->isRealType()) {
Steve Naroff95af0132007-03-30 23:47:58 +0000899 // FIXME: Allow Complex as a GCC extension.
Steve Naroff71ce2e02007-05-18 22:53:50 +0000900 Diag(OpLoc, diag::err_typecheck_illegal_increment_decrement,
901 resType.getAsString(), op->getSourceRange());
Steve Naroff35d85152007-05-07 00:24:15 +0000902 return QualType();
Steve Naroff46ba1eb2007-04-03 23:13:13 +0000903 }
Steve Naroff094046f2007-05-13 03:21:25 +0000904 // At this point, we know we have a real or pointer type. Now make sure
905 // the operand is a modifiable lvalue.
906 if (!op->isModifiableLvalue()) {
Steve Naroff71ce2e02007-05-18 22:53:50 +0000907 Diag(OpLoc, diag::err_typecheck_invalid_lvalue_incr_decr,
Chris Lattner84e160a2007-05-19 07:03:17 +0000908 op->getSourceRange());
Steve Naroff35d85152007-05-07 00:24:15 +0000909 return QualType();
910 }
911 return resType;
Steve Naroff26c8ea52007-03-21 21:08:52 +0000912}
913
Steve Naroff1926c832007-04-24 00:23:05 +0000914/// getPrimaryDeclaration - Helper function for CheckAddressOfOperand().
Steve Naroff47500512007-04-19 23:00:49 +0000915/// This routine allows us to typecheck complex/recursive expressions
916/// where the declaration is needed for type checking. Here are some
917/// examples: &s.xx, &s.zz[1].yy, &(1+2), &(XX), &"123"[2].
Steve Naroff1926c832007-04-24 00:23:05 +0000918static Decl *getPrimaryDeclaration(Expr *e) {
Steve Naroff47500512007-04-19 23:00:49 +0000919 switch (e->getStmtClass()) {
920 case Stmt::DeclRefExprClass:
921 return cast<DeclRefExpr>(e)->getDecl();
922 case Stmt::MemberExprClass:
Steve Naroff1926c832007-04-24 00:23:05 +0000923 return getPrimaryDeclaration(cast<MemberExpr>(e)->getBase());
Steve Naroff47500512007-04-19 23:00:49 +0000924 case Stmt::ArraySubscriptExprClass:
Steve Naroff1926c832007-04-24 00:23:05 +0000925 return getPrimaryDeclaration(cast<ArraySubscriptExpr>(e)->getBase());
Steve Naroff47500512007-04-19 23:00:49 +0000926 case Stmt::CallExprClass:
Steve Naroff1926c832007-04-24 00:23:05 +0000927 return getPrimaryDeclaration(cast<CallExpr>(e)->getCallee());
Steve Naroff47500512007-04-19 23:00:49 +0000928 case Stmt::UnaryOperatorClass:
Steve Naroff1926c832007-04-24 00:23:05 +0000929 return getPrimaryDeclaration(cast<UnaryOperator>(e)->getSubExpr());
Steve Naroff47500512007-04-19 23:00:49 +0000930 case Stmt::ParenExprClass:
Steve Naroff1926c832007-04-24 00:23:05 +0000931 return getPrimaryDeclaration(cast<ParenExpr>(e)->getSubExpr());
Steve Naroff47500512007-04-19 23:00:49 +0000932 default:
933 return 0;
934 }
935}
936
937/// CheckAddressOfOperand - The operand of & must be either a function
938/// designator or an lvalue designating an object. If it is an lvalue, the
939/// object cannot be declared with storage class register or be a bit field.
940/// Note: The usual conversions are *not* applied to the operand of the &
Steve Naroffa78fe7e2007-05-16 19:47:19 +0000941/// operator (C99 6.3.2.1p[2-4]), and its result is never an lvalue.
Steve Naroff35d85152007-05-07 00:24:15 +0000942QualType Sema::CheckAddressOfOperand(Expr *op, SourceLocation OpLoc) {
Steve Naroff1926c832007-04-24 00:23:05 +0000943 Decl *dcl = getPrimaryDeclaration(op);
Steve Naroff47500512007-04-19 23:00:49 +0000944
Steve Naroff5dd642e2007-05-14 18:14:51 +0000945 if (!op->isLvalue()) { // C99 6.5.3.2p1
946 if (dcl && isa<FunctionDecl>(dcl)) // allow function designators
947 ;
Steve Naroff35d85152007-05-07 00:24:15 +0000948 else {
Steve Naroff71ce2e02007-05-18 22:53:50 +0000949 Diag(OpLoc, diag::err_typecheck_invalid_lvalue_addrof,
950 op->getSourceRange());
Steve Naroff35d85152007-05-07 00:24:15 +0000951 return QualType();
952 }
Steve Naroff47500512007-04-19 23:00:49 +0000953 } else if (dcl) {
954 // We have an lvalue with a decl. Make sure the decl is not declared
955 // with the register storage-class specifier.
956 if (const VarDecl *vd = dyn_cast<VarDecl>(dcl)) {
Steve Naroff35d85152007-05-07 00:24:15 +0000957 if (vd->getStorageClass() == VarDecl::Register) {
Steve Naroff71ce2e02007-05-18 22:53:50 +0000958 Diag(OpLoc, diag::err_typecheck_address_of_register,
Chris Lattner84e160a2007-05-19 07:03:17 +0000959 op->getSourceRange());
Steve Naroff35d85152007-05-07 00:24:15 +0000960 return QualType();
961 }
Steve Narofff633d092007-04-25 19:01:39 +0000962 } else
963 assert(0 && "Unknown/unexpected decl type");
964
Steve Naroff47500512007-04-19 23:00:49 +0000965 // FIXME: add check for bitfields!
966 }
967 // If the operand has type "type", the result has type "pointer to type".
Steve Naroff35d85152007-05-07 00:24:15 +0000968 return Context.getPointerType(op->getType());
Steve Naroff47500512007-04-19 23:00:49 +0000969}
970
Steve Naroff35d85152007-05-07 00:24:15 +0000971QualType Sema::CheckIndirectionOperand(Expr *op, SourceLocation OpLoc) {
972 QualType qType = UsualUnaryConversion(op->getType());
973
Steve Naroff47500512007-04-19 23:00:49 +0000974 assert(!qType.isNull() && "no type for * expression");
975
Steve Naroff35d85152007-05-07 00:24:15 +0000976 if (PointerType *PT = dyn_cast<PointerType>(qType))
977 return PT->getPointeeType();
Steve Naroff71ce2e02007-05-18 22:53:50 +0000978 Diag(OpLoc, diag::err_typecheck_unary_expr, qType.getAsString(),
Chris Lattner84e160a2007-05-19 07:03:17 +0000979 op->getSourceRange());
Steve Naroff35d85152007-05-07 00:24:15 +0000980 return QualType();
Steve Naroff1926c832007-04-24 00:23:05 +0000981}
Steve Naroff218bc2b2007-05-04 21:54:46 +0000982
983static inline BinaryOperator::Opcode ConvertTokenKindToBinaryOpcode(
984 tok::TokenKind Kind) {
985 BinaryOperator::Opcode Opc;
986 switch (Kind) {
987 default: assert(0 && "Unknown binop!");
988 case tok::star: Opc = BinaryOperator::Mul; break;
989 case tok::slash: Opc = BinaryOperator::Div; break;
990 case tok::percent: Opc = BinaryOperator::Rem; break;
991 case tok::plus: Opc = BinaryOperator::Add; break;
992 case tok::minus: Opc = BinaryOperator::Sub; break;
993 case tok::lessless: Opc = BinaryOperator::Shl; break;
994 case tok::greatergreater: Opc = BinaryOperator::Shr; break;
995 case tok::lessequal: Opc = BinaryOperator::LE; break;
996 case tok::less: Opc = BinaryOperator::LT; break;
997 case tok::greaterequal: Opc = BinaryOperator::GE; break;
998 case tok::greater: Opc = BinaryOperator::GT; break;
999 case tok::exclaimequal: Opc = BinaryOperator::NE; break;
1000 case tok::equalequal: Opc = BinaryOperator::EQ; break;
1001 case tok::amp: Opc = BinaryOperator::And; break;
1002 case tok::caret: Opc = BinaryOperator::Xor; break;
1003 case tok::pipe: Opc = BinaryOperator::Or; break;
1004 case tok::ampamp: Opc = BinaryOperator::LAnd; break;
1005 case tok::pipepipe: Opc = BinaryOperator::LOr; break;
1006 case tok::equal: Opc = BinaryOperator::Assign; break;
1007 case tok::starequal: Opc = BinaryOperator::MulAssign; break;
1008 case tok::slashequal: Opc = BinaryOperator::DivAssign; break;
1009 case tok::percentequal: Opc = BinaryOperator::RemAssign; break;
1010 case tok::plusequal: Opc = BinaryOperator::AddAssign; break;
1011 case tok::minusequal: Opc = BinaryOperator::SubAssign; break;
1012 case tok::lesslessequal: Opc = BinaryOperator::ShlAssign; break;
1013 case tok::greatergreaterequal: Opc = BinaryOperator::ShrAssign; break;
1014 case tok::ampequal: Opc = BinaryOperator::AndAssign; break;
1015 case tok::caretequal: Opc = BinaryOperator::XorAssign; break;
1016 case tok::pipeequal: Opc = BinaryOperator::OrAssign; break;
1017 case tok::comma: Opc = BinaryOperator::Comma; break;
1018 }
1019 return Opc;
1020}
1021
Steve Naroff35d85152007-05-07 00:24:15 +00001022static inline UnaryOperator::Opcode ConvertTokenKindToUnaryOpcode(
1023 tok::TokenKind Kind) {
1024 UnaryOperator::Opcode Opc;
1025 switch (Kind) {
1026 default: assert(0 && "Unknown unary op!");
1027 case tok::plusplus: Opc = UnaryOperator::PreInc; break;
1028 case tok::minusminus: Opc = UnaryOperator::PreDec; break;
1029 case tok::amp: Opc = UnaryOperator::AddrOf; break;
1030 case tok::star: Opc = UnaryOperator::Deref; break;
1031 case tok::plus: Opc = UnaryOperator::Plus; break;
1032 case tok::minus: Opc = UnaryOperator::Minus; break;
1033 case tok::tilde: Opc = UnaryOperator::Not; break;
1034 case tok::exclaim: Opc = UnaryOperator::LNot; break;
1035 case tok::kw_sizeof: Opc = UnaryOperator::SizeOf; break;
1036 case tok::kw___alignof: Opc = UnaryOperator::AlignOf; break;
1037 case tok::kw___real: Opc = UnaryOperator::Real; break;
1038 case tok::kw___imag: Opc = UnaryOperator::Imag; break;
1039 case tok::ampamp: Opc = UnaryOperator::AddrLabel; break;
1040 // FIXME: case tok::kw___extension__:
1041 }
1042 return Opc;
1043}
1044
Steve Naroff218bc2b2007-05-04 21:54:46 +00001045// Binary Operators. 'Tok' is the token for the operator.
1046Action::ExprResult Sema::ParseBinOp(SourceLocation TokLoc, tok::TokenKind Kind,
1047 ExprTy *LHS, ExprTy *RHS) {
1048 BinaryOperator::Opcode Opc = ConvertTokenKindToBinaryOpcode(Kind);
1049 Expr *lhs = (Expr *)LHS, *rhs = (Expr*)RHS;
1050
1051 assert((lhs != 0) && "ParseBinOp(): missing left expression");
1052 assert((rhs != 0) && "ParseBinOp(): missing right expression");
1053
1054 QualType result;
1055
1056 switch (Opc) {
1057 default:
1058 assert(0 && "Unknown binary expr!");
1059 case BinaryOperator::Assign:
Steve Naroff35d85152007-05-07 00:24:15 +00001060 result = CheckAssignmentOperands(lhs, rhs, TokLoc, QualType());
Steve Naroff218bc2b2007-05-04 21:54:46 +00001061 break;
1062 case BinaryOperator::Mul:
1063 case BinaryOperator::Div:
1064 result = CheckMultiplyDivideOperands(lhs, rhs, TokLoc);
1065 break;
1066 case BinaryOperator::Rem:
1067 result = CheckRemainderOperands(lhs, rhs, TokLoc);
1068 break;
1069 case BinaryOperator::Add:
1070 result = CheckAdditionOperands(lhs, rhs, TokLoc);
1071 break;
1072 case BinaryOperator::Sub:
1073 result = CheckSubtractionOperands(lhs, rhs, TokLoc);
1074 break;
1075 case BinaryOperator::Shl:
1076 case BinaryOperator::Shr:
1077 result = CheckShiftOperands(lhs, rhs, TokLoc);
1078 break;
1079 case BinaryOperator::LE:
1080 case BinaryOperator::LT:
1081 case BinaryOperator::GE:
1082 case BinaryOperator::GT:
1083 result = CheckRelationalOperands(lhs, rhs, TokLoc);
1084 break;
1085 case BinaryOperator::EQ:
1086 case BinaryOperator::NE:
1087 result = CheckEqualityOperands(lhs, rhs, TokLoc);
1088 break;
1089 case BinaryOperator::And:
1090 case BinaryOperator::Xor:
1091 case BinaryOperator::Or:
1092 result = CheckBitwiseOperands(lhs, rhs, TokLoc);
1093 break;
1094 case BinaryOperator::LAnd:
1095 case BinaryOperator::LOr:
1096 result = CheckLogicalOperands(lhs, rhs, TokLoc);
1097 break;
1098 case BinaryOperator::MulAssign:
1099 case BinaryOperator::DivAssign:
1100 result = CheckMultiplyDivideOperands(lhs, rhs, TokLoc);
1101 if (result.isNull())
1102 return true;
Steve Naroff35d85152007-05-07 00:24:15 +00001103 result = CheckAssignmentOperands(lhs, rhs, TokLoc, result);
Steve Naroff218bc2b2007-05-04 21:54:46 +00001104 break;
1105 case BinaryOperator::RemAssign:
1106 result = CheckRemainderOperands(lhs, rhs, TokLoc);
1107 if (result.isNull())
1108 return true;
Steve Naroff35d85152007-05-07 00:24:15 +00001109 result = CheckAssignmentOperands(lhs, rhs, TokLoc, result);
Steve Naroff218bc2b2007-05-04 21:54:46 +00001110 break;
1111 case BinaryOperator::AddAssign:
1112 result = CheckAdditionOperands(lhs, rhs, TokLoc);
1113 if (result.isNull())
1114 return true;
Steve Naroff35d85152007-05-07 00:24:15 +00001115 result = CheckAssignmentOperands(lhs, rhs, TokLoc, result);
Steve Naroff218bc2b2007-05-04 21:54:46 +00001116 break;
1117 case BinaryOperator::SubAssign:
1118 result = CheckSubtractionOperands(lhs, rhs, TokLoc);
1119 if (result.isNull())
1120 return true;
Steve Naroff35d85152007-05-07 00:24:15 +00001121 result = CheckAssignmentOperands(lhs, rhs, TokLoc, result);
Steve Naroff218bc2b2007-05-04 21:54:46 +00001122 break;
1123 case BinaryOperator::ShlAssign:
1124 case BinaryOperator::ShrAssign:
1125 result = CheckShiftOperands(lhs, rhs, TokLoc);
1126 if (result.isNull())
1127 return true;
Steve Naroff35d85152007-05-07 00:24:15 +00001128 result = CheckAssignmentOperands(lhs, rhs, TokLoc, result);
Steve Naroff218bc2b2007-05-04 21:54:46 +00001129 break;
1130 case BinaryOperator::AndAssign:
1131 case BinaryOperator::XorAssign:
1132 case BinaryOperator::OrAssign:
1133 result = CheckBitwiseOperands(lhs, rhs, TokLoc);
1134 if (result.isNull())
1135 return true;
Steve Naroff35d85152007-05-07 00:24:15 +00001136 result = CheckAssignmentOperands(lhs, rhs, TokLoc, result);
Steve Naroff218bc2b2007-05-04 21:54:46 +00001137 break;
1138 case BinaryOperator::Comma:
1139 result = CheckCommaOperands(lhs, rhs, TokLoc);
1140 break;
1141 }
1142 if (result.isNull())
1143 return true;
1144 return new BinaryOperator(lhs, rhs, Opc, result);
1145}
1146
Steve Naroff35d85152007-05-07 00:24:15 +00001147// Unary Operators. 'Tok' is the token for the operator.
1148Action::ExprResult Sema::ParseUnaryOp(SourceLocation OpLoc, tok::TokenKind Op,
1149 ExprTy *Input) {
1150 UnaryOperator::Opcode Opc = ConvertTokenKindToUnaryOpcode(Op);
1151 QualType resultType;
1152 switch (Opc) {
1153 default:
1154 assert(0 && "Unimplemented unary expr!");
1155 case UnaryOperator::PreInc:
1156 case UnaryOperator::PreDec:
Steve Naroff71ce2e02007-05-18 22:53:50 +00001157 resultType = CheckIncrementDecrementOperand((Expr *)Input, OpLoc);
Steve Naroff35d85152007-05-07 00:24:15 +00001158 break;
1159 case UnaryOperator::AddrOf:
1160 resultType = CheckAddressOfOperand((Expr *)Input, OpLoc);
1161 break;
1162 case UnaryOperator::Deref:
1163 resultType = CheckIndirectionOperand((Expr *)Input, OpLoc);
1164 break;
1165 case UnaryOperator::Plus:
1166 case UnaryOperator::Minus:
1167 resultType = UsualUnaryConversion(((Expr *)Input)->getType());
1168 if (!resultType->isArithmeticType()) // C99 6.5.3.3p1
Chris Lattnerc04bd6a2007-05-16 18:09:54 +00001169 return Diag(OpLoc, diag::err_typecheck_unary_expr,
1170 resultType.getAsString());
Steve Naroff35d85152007-05-07 00:24:15 +00001171 break;
1172 case UnaryOperator::Not: // bitwise complement
1173 resultType = UsualUnaryConversion(((Expr *)Input)->getType());
1174 if (!resultType->isIntegerType()) // C99 6.5.3.3p1
Chris Lattnerc04bd6a2007-05-16 18:09:54 +00001175 return Diag(OpLoc, diag::err_typecheck_unary_expr,
1176 resultType.getAsString());
Steve Naroff35d85152007-05-07 00:24:15 +00001177 break;
1178 case UnaryOperator::LNot: // logical negation
1179 resultType = UsualUnaryConversion(((Expr *)Input)->getType());
1180 if (!resultType->isScalarType()) // C99 6.5.3.3p1
Chris Lattnerc04bd6a2007-05-16 18:09:54 +00001181 return Diag(OpLoc, diag::err_typecheck_unary_expr,
1182 resultType.getAsString());
Steve Naroff35d85152007-05-07 00:24:15 +00001183 break;
1184 case UnaryOperator::SizeOf:
Steve Naroff043d45d2007-05-15 02:32:35 +00001185 resultType = CheckSizeOfAlignOfOperand(((Expr *)Input)->getType(), OpLoc,
1186 true);
1187 break;
Steve Naroff35d85152007-05-07 00:24:15 +00001188 case UnaryOperator::AlignOf:
Steve Naroff043d45d2007-05-15 02:32:35 +00001189 resultType = CheckSizeOfAlignOfOperand(((Expr *)Input)->getType(), OpLoc,
1190 false);
1191 break;
Steve Naroff35d85152007-05-07 00:24:15 +00001192 }
1193 if (resultType.isNull())
1194 return true;
Steve Naroff509fe022007-05-17 01:16:00 +00001195 return new UnaryOperator((Expr *)Input, Opc, resultType, OpLoc);
Steve Naroff35d85152007-05-07 00:24:15 +00001196}