blob: 67aa839c1220375eace313573b793c8960ca7ec7 [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());
Steve Naroff509fe022007-05-17 01:16:00 +0000120 return ExprResult(new IntegerLiteral(*t-'0', Context.IntTy,
121 Tok.getLocation()));
Steve Narofff2fb89e2007-03-13 20:29:44 +0000122 }
Steve Naroff8160ea22007-03-06 01:09:46 +0000123 SmallString<512> IntegerBuffer;
124 IntegerBuffer.resize(Tok.getLength());
125 const char *ThisTokBegin = &IntegerBuffer[0];
126
127 // Get the spelling of the token, which eliminates trigraphs, etc. Notes:
128 // - We know that ThisTokBuf points to a buffer that is big enough for the
129 // whole token and 'spelled' tokens can only shrink.
130 // - In practice, the local buffer is only used when the spelling doesn't
131 // match the original token (which is rare). The common case simply returns
132 // a pointer to a *constant* buffer (avoiding a copy).
133
134 unsigned ActualLength = PP.getSpelling(Tok, ThisTokBegin);
Steve Naroff09ef4742007-03-09 23:16:33 +0000135 NumericLiteralParser Literal(ThisTokBegin, ThisTokBegin+ActualLength,
Steve Naroff451d8f162007-03-12 23:22:38 +0000136 Tok.getLocation(), PP);
Steve Narofff2fb89e2007-03-13 20:29:44 +0000137 if (Literal.hadError)
138 return ExprResult(true);
139
Steve Naroff09ef4742007-03-09 23:16:33 +0000140 if (Literal.isIntegerLiteral()) {
Steve Naroffe5aa9be2007-04-05 22:36:20 +0000141 QualType t;
Steve Naroff09ef4742007-03-09 23:16:33 +0000142 if (Literal.hasSuffix()) {
143 if (Literal.isLong)
144 t = Literal.isUnsigned ? Context.UnsignedLongTy : Context.LongTy;
145 else if (Literal.isLongLong)
146 t = Literal.isUnsigned ? Context.UnsignedLongLongTy : Context.LongLongTy;
147 else
148 t = Context.UnsignedIntTy;
149 } else {
150 t = Context.IntTy; // implicit type is "int"
151 }
Steve Naroff451d8f162007-03-12 23:22:38 +0000152 uintmax_t val;
153 if (Literal.GetIntegerValue(val)) {
Steve Naroff509fe022007-05-17 01:16:00 +0000154 return new IntegerLiteral(val, t, Tok.getLocation());
Steve Naroff09ef4742007-03-09 23:16:33 +0000155 }
156 } else if (Literal.isFloatingLiteral()) {
Steve Narofff1e53692007-03-23 22:27:02 +0000157 // FIXME: fill in the value and compute the real type...
Steve Naroff509fe022007-05-17 01:16:00 +0000158 return new FloatingLiteral(7.7, Context.FloatTy, Tok.getLocation());
Steve Naroff09ef4742007-03-09 23:16:33 +0000159 }
Steve Narofff2fb89e2007-03-13 20:29:44 +0000160 return ExprResult(true);
Chris Lattnere168f762006-11-10 05:29:30 +0000161}
162
163Action::ExprResult Sema::ParseParenExpr(SourceLocation L, SourceLocation R,
164 ExprTy *Val) {
Steve Naroffae4143e2007-04-26 20:39:23 +0000165 Expr *e = (Expr *)Val;
166 assert((e != 0) && "ParseParenExpr() missing expr");
Steve Naroff53f07dc2007-05-17 21:49:33 +0000167 return new ParenExpr(L, R, e);
Chris Lattnere168f762006-11-10 05:29:30 +0000168}
169
Steve Naroffa78fe7e2007-05-16 19:47:19 +0000170/// The UsualUnaryConversion() function is *not* called by this routine.
171/// See C99 6.3.2.1p[2-4] for more details.
Steve Naroff043d45d2007-05-15 02:32:35 +0000172QualType Sema::CheckSizeOfAlignOfOperand(QualType exprType,
173 SourceLocation OpLoc, bool isSizeof) {
174 // C99 6.5.3.4p1:
175 if (isa<FunctionType>(exprType) && isSizeof)
176 // alignof(function) is allowed.
177 Diag(OpLoc, diag::ext_sizeof_function_type);
178 else if (exprType->isVoidType())
179 Diag(OpLoc, diag::ext_sizeof_void_type, isSizeof ? "sizeof" : "__alignof");
180 else if (exprType->isIncompleteType()) {
Steve Naroff043d45d2007-05-15 02:32:35 +0000181 Diag(OpLoc, isSizeof ? diag::err_sizeof_incomplete_type :
Chris Lattner3dc3d772007-05-16 18:07:12 +0000182 diag::err_alignof_incomplete_type,
183 exprType.getAsString());
Steve Naroff043d45d2007-05-15 02:32:35 +0000184 return QualType(); // error
185 }
186 // C99 6.5.3.4p4: the type (an unsigned integer type) is size_t.
187 return Context.getSizeType();
188}
189
Chris Lattnere168f762006-11-10 05:29:30 +0000190Action::ExprResult Sema::
191ParseSizeOfAlignOfTypeExpr(SourceLocation OpLoc, bool isSizeof,
Steve Naroff509fe022007-05-17 01:16:00 +0000192 SourceLocation LPLoc, TypeTy *Ty,
193 SourceLocation RPLoc) {
Chris Lattner0d8b1a12006-11-20 04:34:45 +0000194 // If error parsing type, ignore.
195 if (Ty == 0) return true;
Chris Lattner6531c102007-01-23 22:29:49 +0000196
197 // Verify that this is a valid expression.
Steve Naroffe5aa9be2007-04-05 22:36:20 +0000198 QualType ArgTy = QualType::getFromOpaquePtr(Ty);
Chris Lattner6531c102007-01-23 22:29:49 +0000199
Steve Naroff043d45d2007-05-15 02:32:35 +0000200 QualType resultType = CheckSizeOfAlignOfOperand(ArgTy, OpLoc, isSizeof);
201
202 if (resultType.isNull())
203 return true;
Steve Naroff509fe022007-05-17 01:16:00 +0000204 return new SizeOfAlignOfTypeExpr(isSizeof, ArgTy, resultType, OpLoc, RPLoc);
Chris Lattnere168f762006-11-10 05:29:30 +0000205}
206
207
208Action::ExprResult Sema::ParsePostfixUnaryOp(SourceLocation OpLoc,
209 tok::TokenKind Kind,
210 ExprTy *Input) {
211 UnaryOperator::Opcode Opc;
212 switch (Kind) {
213 default: assert(0 && "Unknown unary op!");
214 case tok::plusplus: Opc = UnaryOperator::PostInc; break;
215 case tok::minusminus: Opc = UnaryOperator::PostDec; break;
216 }
Steve Naroff71ce2e02007-05-18 22:53:50 +0000217 QualType result = CheckIncrementDecrementOperand((Expr *)Input, OpLoc);
Steve Naroff35d85152007-05-07 00:24:15 +0000218 if (result.isNull())
219 return true;
Steve Naroff509fe022007-05-17 01:16:00 +0000220 return new UnaryOperator((Expr *)Input, Opc, result, OpLoc);
Chris Lattnere168f762006-11-10 05:29:30 +0000221}
222
223Action::ExprResult Sema::
224ParseArraySubscriptExpr(ExprTy *Base, SourceLocation LLoc,
225 ExprTy *Idx, SourceLocation RLoc) {
Steve Naroffe5aa9be2007-04-05 22:36:20 +0000226 QualType t1 = ((Expr *)Base)->getType();
227 QualType t2 = ((Expr *)Idx)->getType();
Steve Narofff1e53692007-03-23 22:27:02 +0000228
229 assert(!t1.isNull() && "no type for array base expression");
Steve Naroffc1aadb12007-03-28 21:49:40 +0000230 assert(!t2.isNull() && "no type for array index expression");
Steve Narofff1e53692007-03-23 22:27:02 +0000231
Steve Naroffe5aa9be2007-04-05 22:36:20 +0000232 QualType canonT1 = t1.getCanonicalType();
233 QualType canonT2 = t2.getCanonicalType();
Steve Naroffd50c88e2007-04-05 21:15:20 +0000234
Steve Naroffc1aadb12007-03-28 21:49:40 +0000235 // C99 6.5.2.1p2: the expression e1[e2] is by definition precisely equivalent
236 // to the expression *((e1)+(e2)). This means the array "Base" may actually be
Steve Narofff1e53692007-03-23 22:27:02 +0000237 // in the subscript position. As a result, we need to derive the array base
238 // and index from the expression types.
239
Steve Naroffe5aa9be2007-04-05 22:36:20 +0000240 QualType baseType, indexType;
Steve Naroffd50c88e2007-04-05 21:15:20 +0000241 if (isa<ArrayType>(canonT1) || isa<PointerType>(canonT1)) {
242 baseType = canonT1;
243 indexType = canonT2;
244 } else if (isa<ArrayType>(canonT2) || isa<PointerType>(canonT2)) { // uncommon
245 baseType = canonT2;
246 indexType = canonT1;
Steve Narofff1e53692007-03-23 22:27:02 +0000247 } else
248 return Diag(LLoc, diag::err_typecheck_subscript_value);
249
Steve Naroffc1aadb12007-03-28 21:49:40 +0000250 // C99 6.5.2.1p1
Steve Naroff1926c832007-04-24 00:23:05 +0000251 if (!indexType->isIntegerType())
Steve Narofff1e53692007-03-23 22:27:02 +0000252 return Diag(LLoc, diag::err_typecheck_subscript);
Steve Naroffc1aadb12007-03-28 21:49:40 +0000253
Steve Naroff95af0132007-03-30 23:47:58 +0000254 // FIXME: need to deal with const...
Steve Naroffe5aa9be2007-04-05 22:36:20 +0000255 QualType resultType;
Steve Naroffc1aadb12007-03-28 21:49:40 +0000256 if (ArrayType *ary = dyn_cast<ArrayType>(baseType)) {
257 resultType = ary->getElementType();
258 } else if (PointerType *ary = dyn_cast<PointerType>(baseType)) {
259 resultType = ary->getPointeeType();
260 // in practice, the following check catches trying to index a pointer
261 // to a function (e.g. void (*)(int)). Functions are not objects in c99.
Steve Naroffbc2f0992007-03-30 20:09:34 +0000262 if (!resultType->isObjectType())
Chris Lattnerc04bd6a2007-05-16 18:09:54 +0000263 return Diag(LLoc, diag::err_typecheck_subscript_not_object,
264 baseType.getAsString());
Steve Naroffc1aadb12007-03-28 21:49:40 +0000265 }
Steve Naroff509fe022007-05-17 01:16:00 +0000266 return new ArraySubscriptExpr((Expr*)Base, (Expr*)Idx, resultType, RLoc);
Chris Lattnere168f762006-11-10 05:29:30 +0000267}
268
269Action::ExprResult Sema::
270ParseMemberReferenceExpr(ExprTy *Base, SourceLocation OpLoc,
271 tok::TokenKind OpKind, SourceLocation MemberLoc,
272 IdentifierInfo &Member) {
Steve Naroffe5aa9be2007-04-05 22:36:20 +0000273 QualType qualifiedType = ((Expr *)Base)->getType();
Steve Naroffca8f7122007-04-01 01:41:35 +0000274
275 assert(!qualifiedType.isNull() && "no type for member expression");
276
Steve Naroffe5aa9be2007-04-05 22:36:20 +0000277 QualType canonType = qualifiedType.getCanonicalType();
Steve Narofff1e53692007-03-23 22:27:02 +0000278
279 if (OpKind == tok::arrow) {
Steve Naroffca8f7122007-04-01 01:41:35 +0000280 if (PointerType *PT = dyn_cast<PointerType>(canonType)) {
281 qualifiedType = PT->getPointeeType();
Steve Naroffd50c88e2007-04-05 21:15:20 +0000282 canonType = qualifiedType.getCanonicalType();
Steve Naroffca8f7122007-04-01 01:41:35 +0000283 } else
Steve Narofff1e53692007-03-23 22:27:02 +0000284 return Diag(OpLoc, diag::err_typecheck_member_reference_arrow);
285 }
Steve Naroff92e30f82007-04-02 22:35:25 +0000286 if (!isa<RecordType>(canonType))
287 return Diag(OpLoc, diag::err_typecheck_member_reference_structUnion);
288
289 // get the struct/union definition from the type.
290 RecordDecl *RD = cast<RecordType>(canonType)->getDecl();
Steve Naroffcc321422007-03-26 23:09:51 +0000291
Steve Naroff92e30f82007-04-02 22:35:25 +0000292 if (canonType->isIncompleteType())
293 return Diag(OpLoc, diag::err_typecheck_incomplete_tag, RD->getName());
Steve Naroffcc321422007-03-26 23:09:51 +0000294
Steve Naroff92e30f82007-04-02 22:35:25 +0000295 FieldDecl *MemberDecl = RD->getMember(&Member);
296 if (!MemberDecl)
297 return Diag(OpLoc, diag::err_typecheck_no_member, Member.getName());
298
299 return new MemberExpr((Expr*)Base, OpKind == tok::arrow, MemberDecl);
Chris Lattnere168f762006-11-10 05:29:30 +0000300}
301
302/// ParseCallExpr - Handle a call to Fn with the specified array of arguments.
303/// This provides the location of the left/right parens and a list of comma
304/// locations.
305Action::ExprResult Sema::
306ParseCallExpr(ExprTy *Fn, SourceLocation LParenLoc,
Steve Naroffb8c289d2007-05-08 22:18:00 +0000307 ExprTy **Args, unsigned NumArgsInCall,
Chris Lattnere168f762006-11-10 05:29:30 +0000308 SourceLocation *CommaLocs, SourceLocation RParenLoc) {
Steve Naroffae4143e2007-04-26 20:39:23 +0000309 QualType qType = ((Expr *)Fn)->getType();
310
311 assert(!qType.isNull() && "no type for function call expression");
312
Steve Naroff17f76e02007-05-03 21:03:48 +0000313 const FunctionType *funcT = dyn_cast<FunctionType>(qType.getCanonicalType());
Steve Naroffae4143e2007-04-26 20:39:23 +0000314
Steve Naroff17f76e02007-05-03 21:03:48 +0000315 assert(funcT && "ParseCallExpr(): not a function type");
Steve Naroffb8c289d2007-05-08 22:18:00 +0000316
Steve Naroff17f76e02007-05-03 21:03:48 +0000317 // If a prototype isn't declared, the parser implicitly defines a func decl
318 QualType resultType = funcT->getResultType();
319
320 if (const FunctionTypeProto *proto = dyn_cast<FunctionTypeProto>(funcT)) {
321 // C99 6.5.2.2p7 - the arguments are implicitly converted, as if by
322 // assignment, to the types of the corresponding parameter, ...
323
324 unsigned NumArgsInProto = proto->getNumArgs();
Steve Naroffb8c289d2007-05-08 22:18:00 +0000325 unsigned NumArgsToCheck = NumArgsInCall;
Steve Naroff17f76e02007-05-03 21:03:48 +0000326
Steve Naroffb8c289d2007-05-08 22:18:00 +0000327 if (NumArgsInCall < NumArgsInProto)
Steve Naroff1f4d7272007-05-11 04:00:31 +0000328 Diag(LParenLoc, diag::err_typecheck_call_too_few_args);
Steve Naroffb8c289d2007-05-08 22:18:00 +0000329 else if (NumArgsInCall > NumArgsInProto) {
330 if (!proto->isVariadic())
Steve Naroff1f4d7272007-05-11 04:00:31 +0000331 Diag(LParenLoc, diag::err_typecheck_call_too_many_args);
Steve Naroffb8c289d2007-05-08 22:18:00 +0000332 NumArgsToCheck = NumArgsInProto;
Steve Naroff17f76e02007-05-03 21:03:48 +0000333 }
334 // Continue to check argument types (even if we have too few/many args).
Steve Naroffb8c289d2007-05-08 22:18:00 +0000335 for (unsigned i = 0; i < NumArgsToCheck; i++) {
Steve Naroff17f76e02007-05-03 21:03:48 +0000336 QualType lhsType = proto->getArgType(i);
337 QualType rhsType = ((Expr **)Args)[i]->getType();
338
339 if (lhsType == rhsType) // common case, fast path...
340 continue;
341 AssignmentConversionResult result;
Steve Naroff218bc2b2007-05-04 21:54:46 +0000342 UsualAssignmentConversions(lhsType, rhsType, result);
Steve Naroff17f76e02007-05-03 21:03:48 +0000343
344 SourceLocation l = (i == 0) ? LParenLoc : CommaLocs[i-1];
345
346 // decode the result (notice that AST's are still created for extensions).
347 // FIXME: consider fancier error diagnostics (since this is quite common).
348 // #1: emit the actual prototype arg...requires adding source loc info.
349 // #2: pass Diag the offending argument type...requires hacking Diag.
350 switch (result) {
351 case Compatible:
352 break;
353 case PointerFromInt:
Steve Naroff218bc2b2007-05-04 21:54:46 +0000354 // check for null pointer constant (C99 6.3.2.3p3)
355 if (!((Expr **)Args)[i]->isNullPointerConstant())
356 Diag(l, diag::ext_typecheck_passing_pointer_from_int, utostr(i+1));
Steve Naroff17f76e02007-05-03 21:03:48 +0000357 break;
358 case IntFromPointer:
359 Diag(l, diag::ext_typecheck_passing_int_from_pointer, utostr(i+1));
360 break;
361 case IncompatiblePointer:
362 Diag(l, diag::ext_typecheck_passing_incompatible_pointer, utostr(i+1));
363 break;
Steve Naroff1f4d7272007-05-11 04:00:31 +0000364 case CompatiblePointerDiscardsQualifiers:
365 Diag(l, diag::ext_typecheck_passing_discards_qualifiers, utostr(i+1));
366 break;
Steve Naroff17f76e02007-05-03 21:03:48 +0000367 case Incompatible:
368 return Diag(l, diag::err_typecheck_passing_incompatible, utostr(i+1));
369 }
370 }
Steve Naroffb8c289d2007-05-08 22:18:00 +0000371 // Even if the types checked, bail if we had the wrong number of arguments.
372 if ((NumArgsInCall != NumArgsInProto) && !proto->isVariadic())
373 return true;
Steve Naroffae4143e2007-04-26 20:39:23 +0000374 }
Steve Naroff509fe022007-05-17 01:16:00 +0000375 return new CallExpr((Expr*)Fn, (Expr**)Args, NumArgsInCall, resultType,
376 RParenLoc);
Chris Lattnere168f762006-11-10 05:29:30 +0000377}
378
379Action::ExprResult Sema::
380ParseCastExpr(SourceLocation LParenLoc, TypeTy *Ty,
381 SourceLocation RParenLoc, ExprTy *Op) {
Chris Lattner0d8b1a12006-11-20 04:34:45 +0000382 // If error parsing type, ignore.
Steve Naroffae4143e2007-04-26 20:39:23 +0000383 assert((Ty != 0) && "ParseCastExpr(): missing type");
Steve Naroff509fe022007-05-17 01:16:00 +0000384 return new CastExpr(QualType::getFromOpaquePtr(Ty), (Expr*)Op, LParenLoc);
Chris Lattnere168f762006-11-10 05:29:30 +0000385}
386
Steve Narofff8a28c52007-05-15 20:29:32 +0000387inline QualType Sema::CheckConditionalOperands( // C99 6.5.15
388 Expr *Cond, Expr *LHS, Expr *RHS, SourceLocation questionLoc) {
Steve Naroffa78fe7e2007-05-16 19:47:19 +0000389 QualType cond = Cond->getType();
390 QualType lhs = LHS->getType();
391 QualType rhs = RHS->getType();
392
Steve Narofff8a28c52007-05-15 20:29:32 +0000393 assert(!cond.isNull() && "ParseConditionalOp(): no conditional type");
394 assert(!lhs.isNull() && "ParseConditionalOp(): no lhs type");
395 assert(!rhs.isNull() && "ParseConditionalOp(): no rhs type");
396
Steve Naroffa78fe7e2007-05-16 19:47:19 +0000397 cond = UsualUnaryConversion(cond);
398 lhs = UsualUnaryConversion(lhs);
399 rhs = UsualUnaryConversion(rhs);
400
401 // first, check the condition.
402 if (!cond->isScalarType()) { // C99 6.5.15p2
Steve Naroff53f07dc2007-05-17 21:49:33 +0000403 Diag(Cond->getLocStart(), diag::err_typecheck_cond_expect_scalar,
Steve Naroff509fe022007-05-17 01:16:00 +0000404 cond.getAsString());
Steve Naroffa78fe7e2007-05-16 19:47:19 +0000405 return QualType();
406 }
407 // now check the two expressions.
408 if (lhs->isArithmeticType() && rhs->isArithmeticType()) // C99 6.5.15p3,5
409 return UsualArithmeticConversions(lhs, rhs);
410
411 if ((lhs->isStructureType() && rhs->isStructureType()) || // C99 6.5.15p3
412 (lhs->isUnionType() && rhs->isUnionType())) {
413 TagType *lTag = cast<TagType>(lhs.getCanonicalType());
414 TagType *rTag = cast<TagType>(rhs.getCanonicalType());
415
416 if (lTag->getDecl()->getIdentifier() == rTag->getDecl()->getIdentifier())
417 return lhs;
418 else {
419 Diag(questionLoc, diag::err_typecheck_cond_incompatible_operands,
420 lhs.getAsString(), rhs.getAsString());
421 return QualType();
422 }
423 }
Steve Naroff30d1fbc2007-05-20 19:46:53 +0000424 if (lhs->isPointerType() && RHS->isNullPointerConstant()) // C99 6.5.15p3
425 return lhs;
426 if (rhs->isPointerType() && LHS->isNullPointerConstant())
427 return rhs;
428
Steve Naroffa78fe7e2007-05-16 19:47:19 +0000429 if (lhs->isPointerType() && rhs->isPointerType()) { // C99 6.5.15p3,6
430 QualType lhptee, rhptee;
431
432 // get the "pointed to" type
433 lhptee = cast<PointerType>(lhs.getCanonicalType())->getPointeeType();
434 rhptee = cast<PointerType>(rhs.getCanonicalType())->getPointeeType();
435
436 // ignore qualifiers on void (C99 6.5.15p3, clause 6)
437 if (lhptee.getUnqualifiedType()->isVoidType() &&
438 (rhptee->isObjectType() || rhptee->isIncompleteType()))
439 return lhs;
440 if (rhptee.getUnqualifiedType()->isVoidType() &&
441 (lhptee->isObjectType() || lhptee->isIncompleteType()))
442 return rhs;
443
444 // FIXME: C99 6.5.15p6: If both operands are pointers to compatible types
445 // *or* to differently qualified versions of compatible types, the result
446 // type is a pointer to an appropriately qualified version of the
447 // *composite* type.
448 if (!Type::typesAreCompatible(lhptee.getUnqualifiedType(),
449 rhptee.getUnqualifiedType())) {
450 Diag(questionLoc, diag::ext_typecheck_cond_incompatible_pointers,
451 lhs.getAsString(), rhs.getAsString());
452 return lhs; // FIXME: this is an _ext - is this return o.k?
453 }
454 }
455 if (lhs->isVoidType() && rhs->isVoidType()) // C99 6.5.15p3
456 return lhs;
Steve Naroffa78fe7e2007-05-16 19:47:19 +0000457
458 Diag(questionLoc, diag::err_typecheck_cond_incompatible_operands,
459 lhs.getAsString(), rhs.getAsString());
460 return QualType();
Steve Narofff8a28c52007-05-15 20:29:32 +0000461}
462
Chris Lattnere168f762006-11-10 05:29:30 +0000463/// ParseConditionalOp - Parse a ?: operation. Note that 'LHS' may be null
464/// in the case of a the GNU conditional expr extension.
465Action::ExprResult Sema::ParseConditionalOp(SourceLocation QuestionLoc,
466 SourceLocation ColonLoc,
467 ExprTy *Cond, ExprTy *LHS,
468 ExprTy *RHS) {
Steve Narofff8a28c52007-05-15 20:29:32 +0000469 QualType result = CheckConditionalOperands((Expr *)Cond, (Expr *)LHS,
470 (Expr *)RHS, QuestionLoc);
471 if (result.isNull())
472 return true;
473 return new ConditionalOperator((Expr*)Cond, (Expr*)LHS, (Expr*)RHS, result);
Chris Lattnere168f762006-11-10 05:29:30 +0000474}
475
Steve Naroff1926c832007-04-24 00:23:05 +0000476/// UsualUnaryConversion - Performs various conversions that are common to most
477/// operators (C99 6.3). The conversions of array and function types are
478/// sometimes surpressed. For example, the array->pointer conversion doesn't
479/// apply if the array is an argument to the sizeof or address (&) operators.
480/// In these instances, this routine should *not* be called.
481QualType Sema::UsualUnaryConversion(QualType t) {
482 assert(!t.isNull() && "UsualUnaryConversion - missing type");
Steve Naroff4b7ce032007-04-20 22:26:17 +0000483
Steve Naroff1926c832007-04-24 00:23:05 +0000484 if (t->isPromotableIntegerType()) // C99 6.3.1.1p2
485 return Context.IntTy;
Steve Naroffa78fe7e2007-05-16 19:47:19 +0000486 if (t->isFunctionType()) // C99 6.3.2.1p4
487 return Context.getPointerType(t.getCanonicalType());
488 if (const ArrayType *ary = dyn_cast<ArrayType>(t.getCanonicalType()))
489 return Context.getPointerType(ary->getElementType()); // C99 6.3.2.1p3
Steve Naroff1926c832007-04-24 00:23:05 +0000490 return t;
491}
492
493/// UsualArithmeticConversions - Performs various conversions that are common to
494/// binary operators (C99 6.3.1.8). If both operands aren't arithmetic, this
495/// routine returns the first non-arithmetic type found. The client is
496/// responsible for emitting appropriate error diagnostics.
497QualType Sema::UsualArithmeticConversions(QualType t1, QualType t2) {
Steve Naroffb01bbe32007-04-25 01:22:31 +0000498 QualType lhs = UsualUnaryConversion(t1);
499 QualType rhs = UsualUnaryConversion(t2);
Steve Naroff1926c832007-04-24 00:23:05 +0000500
501 // if either operand is not of arithmetic type, no conversion is possible.
Steve Naroffb01bbe32007-04-25 01:22:31 +0000502 if (!lhs->isArithmeticType())
503 return lhs;
Steve Narofff633d092007-04-25 19:01:39 +0000504 if (!rhs->isArithmeticType())
Steve Naroffb01bbe32007-04-25 01:22:31 +0000505 return rhs;
Steve Naroff1926c832007-04-24 00:23:05 +0000506
Steve Narofff633d092007-04-25 19:01:39 +0000507 // if both arithmetic types are identical, no conversion is needed.
Steve Naroffb01bbe32007-04-25 01:22:31 +0000508 if (lhs == rhs)
509 return lhs;
Steve Naroff1926c832007-04-24 00:23:05 +0000510
Steve Naroffb01bbe32007-04-25 01:22:31 +0000511 // at this point, we have two different arithmetic types.
512
513 // Handle complex types first (C99 6.3.1.8p1).
514 if (lhs->isComplexType() || rhs->isComplexType()) {
515 // if we have an integer operand, the result is the complex type.
516 if (rhs->isIntegerType())
517 return lhs;
518 if (lhs->isIntegerType())
519 return rhs;
520
Steve Naroffe4718892007-04-27 18:30:00 +0000521 return Context.maxComplexType(lhs, rhs);
Steve Naroffbf223ba2007-04-24 20:56:26 +0000522 }
Steve Naroffb01bbe32007-04-25 01:22:31 +0000523 // Now handle "real" floating types (i.e. float, double, long double).
524 if (lhs->isRealFloatingType() || rhs->isRealFloatingType()) {
525 // if we have an integer operand, the result is the real floating type.
526 if (rhs->isIntegerType())
527 return lhs;
528 if (lhs->isIntegerType())
529 return rhs;
530
531 // we have two real floating types, float/complex combos were handled above.
Steve Naroffe4718892007-04-27 18:30:00 +0000532 return Context.maxFloatingType(lhs, rhs);
Steve Naroffb01bbe32007-04-25 01:22:31 +0000533 }
Steve Naroffe4718892007-04-27 18:30:00 +0000534 return Context.maxIntegerType(lhs, rhs);
Steve Narofff1e53692007-03-23 22:27:02 +0000535}
536
Steve Naroff3f597292007-05-11 22:18:03 +0000537// CheckPointerTypesForAssignment - This is a very tricky routine (despite
538// being closely modeled after the C99 spec:-). The odd characteristic of this
539// routine is it effectively iqnores the qualifiers on the top level pointee.
540// This circumvents the usual type rules specified in 6.2.7p1 & 6.7.5.[1-3].
541// FIXME: add a couple examples in this comment.
542QualType Sema::CheckPointerTypesForAssignment(QualType lhsType,
543 QualType rhsType,
544 AssignmentConversionResult &r) {
545 QualType lhptee, rhptee;
Steve Naroff1f4d7272007-05-11 04:00:31 +0000546
547 // get the "pointed to" type (ignoring qualifiers at the top level)
Steve Naroff3f597292007-05-11 22:18:03 +0000548 lhptee = cast<PointerType>(lhsType.getCanonicalType())->getPointeeType();
549 rhptee = cast<PointerType>(rhsType.getCanonicalType())->getPointeeType();
Steve Naroff1f4d7272007-05-11 04:00:31 +0000550
551 // make sure we operate on the canonical type
Steve Naroff3f597292007-05-11 22:18:03 +0000552 lhptee = lhptee.getCanonicalType();
553 rhptee = rhptee.getCanonicalType();
Steve Naroff1f4d7272007-05-11 04:00:31 +0000554
Steve Naroff3f597292007-05-11 22:18:03 +0000555 // C99 6.5.16.1p1: This following citation is common to constraints
556 // 3 & 4 (below). ...and the type *pointed to* by the left has all the
557 // qualifiers of the type *pointed to* by the right;
558 if ((lhptee.getQualifiers() & rhptee.getQualifiers()) !=
559 rhptee.getQualifiers())
560 r = CompatiblePointerDiscardsQualifiers;
561
562 // C99 6.5.16.1p1 (constraint 4): If one operand is a pointer to an object or
563 // incomplete type and the other is a pointer to a qualified or unqualified
564 // version of void...
565 if (lhptee.getUnqualifiedType()->isVoidType() &&
566 (rhptee->isObjectType() || rhptee->isIncompleteType()))
567 ;
568 else if (rhptee.getUnqualifiedType()->isVoidType() &&
569 (lhptee->isObjectType() || lhptee->isIncompleteType()))
570 ;
571 // C99 6.5.16.1p1 (constraint 3): both operands are pointers to qualified or
572 // unqualified versions of compatible types, ...
573 else if (!Type::typesAreCompatible(lhptee.getUnqualifiedType(),
574 rhptee.getUnqualifiedType()))
575 r = IncompatiblePointer; // this "trumps" PointerAssignDiscardsQualifiers
576 return rhsType;
Steve Naroff1f4d7272007-05-11 04:00:31 +0000577}
578
Steve Naroff17f76e02007-05-03 21:03:48 +0000579/// UsualAssignmentConversions (C99 6.5.16) - This routine currently
580/// has code to accommodate several GCC extensions when type checking
581/// pointers. Here are some objectionable examples that GCC considers warnings:
582///
583/// int a, *pint;
584/// short *pshort;
585/// struct foo *pfoo;
586///
587/// pint = pshort; // warning: assignment from incompatible pointer type
588/// a = pint; // warning: assignment makes integer from pointer without a cast
589/// pint = a; // warning: assignment makes pointer from integer without a cast
590/// pint = pfoo; // warning: assignment from incompatible pointer type
591///
592/// As a result, the code for dealing with pointers is more complex than the
593/// C99 spec dictates.
594/// Note: the warning above turn into errors when -pedantic-errors is enabled.
595///
Steve Naroff218bc2b2007-05-04 21:54:46 +0000596QualType Sema::UsualAssignmentConversions(QualType lhsType, QualType rhsType,
Steve Naroff17f76e02007-05-03 21:03:48 +0000597 AssignmentConversionResult &r) {
Steve Naroffb891de32007-05-02 23:51:10 +0000598 // this check seems unnatural, however it necessary to insure the proper
599 // conversion of functions/arrays. If the conversion where done for all
600 // DeclExpr's (created by ParseIdentifierExpr), it would mess up the
601 // unary expressions that surpress this implicit conversion (&, sizeof).
602 if (rhsType->isFunctionType() || rhsType->isArrayType())
603 rhsType = UsualUnaryConversion(rhsType);
604
Steve Naroff17f76e02007-05-03 21:03:48 +0000605 r = Compatible;
Steve Naroff9eb24652007-05-02 21:58:15 +0000606 if (lhsType->isArithmeticType() && rhsType->isArithmeticType())
607 return lhsType;
608 else if (lhsType->isPointerType()) {
609 if (rhsType->isIntegerType()) {
Steve Naroff218bc2b2007-05-04 21:54:46 +0000610 r = PointerFromInt;
Steve Naroffb891de32007-05-02 23:51:10 +0000611 return rhsType;
Steve Naroff9eb24652007-05-02 21:58:15 +0000612 }
Steve Naroff3f597292007-05-11 22:18:03 +0000613 if (rhsType->isPointerType())
614 return CheckPointerTypesForAssignment(lhsType, rhsType, r);
Steve Naroff9eb24652007-05-02 21:58:15 +0000615 } else if (rhsType->isPointerType()) {
616 if (lhsType->isIntegerType()) {
617 // C99 6.5.16.1p1: the left operand is _Bool and the right is a pointer.
618 if (lhsType != Context.BoolTy)
Steve Naroff17f76e02007-05-03 21:03:48 +0000619 r = IntFromPointer;
Steve Naroffb891de32007-05-02 23:51:10 +0000620 return rhsType;
Steve Naroff9eb24652007-05-02 21:58:15 +0000621 }
Steve Naroff3f597292007-05-11 22:18:03 +0000622 if (lhsType->isPointerType())
623 return CheckPointerTypesForAssignment(lhsType, rhsType, r);
Steve Naroff1f4d7272007-05-11 04:00:31 +0000624 } else if (isa<TagType>(lhsType) && isa<TagType>(rhsType)) {
625 if (Type::tagTypesAreCompatible(lhsType, rhsType))
Steve Naroffb891de32007-05-02 23:51:10 +0000626 return rhsType;
Steve Naroff1f4d7272007-05-11 04:00:31 +0000627 }
Steve Naroff17f76e02007-05-03 21:03:48 +0000628 r = Incompatible;
629 return QualType();
Steve Naroff9eb24652007-05-02 21:58:15 +0000630}
631
Steve Naroff218bc2b2007-05-04 21:54:46 +0000632inline QualType Sema::CheckMultiplyDivideOperands(
633 Expr *lex, Expr *rex, SourceLocation loc)
Steve Naroff5c10d4b2007-04-20 23:42:24 +0000634{
Steve Naroff1926c832007-04-24 00:23:05 +0000635 QualType resType = UsualArithmeticConversions(lex->getType(), rex->getType());
Steve Naroff5c10d4b2007-04-20 23:42:24 +0000636
Steve Naroff218bc2b2007-05-04 21:54:46 +0000637 if (resType->isArithmeticType())
638 return resType;
Steve Naroffe845e272007-05-18 01:06:45 +0000639 Diag(loc, diag::err_typecheck_invalid_operands,
Chris Lattner84e160a2007-05-19 07:03:17 +0000640 lex->getType().getAsString(), rex->getType().getAsString(),
641 lex->getSourceRange(), rex->getSourceRange());
Steve Naroff218bc2b2007-05-04 21:54:46 +0000642 return QualType();
Steve Naroff26c8ea52007-03-21 21:08:52 +0000643}
644
Steve Naroff218bc2b2007-05-04 21:54:46 +0000645inline QualType Sema::CheckRemainderOperands(
646 Expr *lex, Expr *rex, SourceLocation loc)
647{
648 QualType resType = UsualArithmeticConversions(lex->getType(), rex->getType());
649
650 if (resType->isIntegerType())
651 return resType;
Steve Naroffe845e272007-05-18 01:06:45 +0000652 Diag(loc, diag::err_typecheck_invalid_operands,
Chris Lattner84e160a2007-05-19 07:03:17 +0000653 lex->getType().getAsString(), rex->getType().getAsString(),
654 lex->getSourceRange(), rex->getSourceRange());
Steve Naroff218bc2b2007-05-04 21:54:46 +0000655 return QualType();
656}
657
658inline QualType Sema::CheckAdditionOperands( // C99 6.5.6
659 Expr *lex, Expr *rex, SourceLocation loc)
Steve Naroff1926c832007-04-24 00:23:05 +0000660{
Steve Naroffe4718892007-04-27 18:30:00 +0000661 QualType lhsType = lex->getType(), rhsType = rex->getType();
662 QualType resType = UsualArithmeticConversions(lhsType, rhsType);
663
664 // handle the common case first (both operands are arithmetic).
665 if (resType->isArithmeticType())
Steve Naroff218bc2b2007-05-04 21:54:46 +0000666 return resType;
667
668 if ((lhsType->isPointerType() && rhsType->isIntegerType()) ||
669 (lhsType->isIntegerType() && rhsType->isPointerType()))
670 return resType;
Steve Naroffe845e272007-05-18 01:06:45 +0000671 Diag(loc, diag::err_typecheck_invalid_operands,
Chris Lattner84e160a2007-05-19 07:03:17 +0000672 lhsType.getAsString(), rhsType.getAsString(),
673 lex->getSourceRange(), rex->getSourceRange());
Steve Naroff218bc2b2007-05-04 21:54:46 +0000674 return QualType();
Steve Naroff26c8ea52007-03-21 21:08:52 +0000675}
676
Steve Naroff218bc2b2007-05-04 21:54:46 +0000677inline QualType Sema::CheckSubtractionOperands( // C99 6.5.6
678 Expr *lex, Expr *rex, SourceLocation loc)
679{
680 QualType lhsType = lex->getType(), rhsType = rex->getType();
681 QualType resType = UsualArithmeticConversions(lhsType, rhsType);
682
683 // handle the common case first (both operands are arithmetic).
684 if (resType->isArithmeticType())
685 return resType;
686 if ((lhsType->isPointerType() && rhsType->isIntegerType()) ||
687 (lhsType->isPointerType() && rhsType->isPointerType()))
688 return resType;
Steve Naroffe845e272007-05-18 01:06:45 +0000689 Diag(loc, diag::err_typecheck_invalid_operands,
Chris Lattner84e160a2007-05-19 07:03:17 +0000690 lhsType.getAsString(), rhsType.getAsString(),
691 lex->getSourceRange(), rex->getSourceRange());
Steve Naroff218bc2b2007-05-04 21:54:46 +0000692 return QualType();
693}
694
695inline QualType Sema::CheckShiftOperands( // C99 6.5.7
696 Expr *lex, Expr *rex, SourceLocation loc)
Steve Naroff1926c832007-04-24 00:23:05 +0000697{
698 QualType resType = UsualArithmeticConversions(lex->getType(), rex->getType());
699
Steve Naroff218bc2b2007-05-04 21:54:46 +0000700 if (resType->isIntegerType())
701 return resType;
Steve Naroffe845e272007-05-18 01:06:45 +0000702 Diag(loc, diag::err_typecheck_invalid_operands,
Chris Lattner84e160a2007-05-19 07:03:17 +0000703 lex->getType().getAsString(), rex->getType().getAsString(),
704 lex->getSourceRange(), rex->getSourceRange());
Steve Naroff218bc2b2007-05-04 21:54:46 +0000705 return QualType();
Steve Naroff26c8ea52007-03-21 21:08:52 +0000706}
707
Steve Naroff218bc2b2007-05-04 21:54:46 +0000708inline QualType Sema::CheckRelationalOperands( // C99 6.5.8
709 Expr *lex, Expr *rex, SourceLocation loc)
Steve Naroff1926c832007-04-24 00:23:05 +0000710{
711 QualType lType = lex->getType(), rType = rex->getType();
712
713 if (lType->isRealType() && rType->isRealType())
Steve Naroff218bc2b2007-05-04 21:54:46 +0000714 return Context.IntTy;
Steve Naroffe4718892007-04-27 18:30:00 +0000715
716 if (lType->isPointerType() && rType->isPointerType())
Steve Naroff218bc2b2007-05-04 21:54:46 +0000717 return Context.IntTy;
Steve Naroffe4718892007-04-27 18:30:00 +0000718
Steve Naroff043d45d2007-05-15 02:32:35 +0000719 if (lType->isIntegerType() || rType->isIntegerType()) { // GCC extension.
Steve Naroff218bc2b2007-05-04 21:54:46 +0000720 Diag(loc, diag::ext_typecheck_comparison_of_pointer_integer);
Steve Naroff043d45d2007-05-15 02:32:35 +0000721 return Context.IntTy;
722 }
Steve Naroffe845e272007-05-18 01:06:45 +0000723 Diag(loc, diag::err_typecheck_invalid_operands,
Chris Lattner84e160a2007-05-19 07:03:17 +0000724 lType.getAsString(), rType.getAsString(),
725 lex->getSourceRange(), rex->getSourceRange());
Steve Naroff218bc2b2007-05-04 21:54:46 +0000726 return QualType();
Steve Naroff26c8ea52007-03-21 21:08:52 +0000727}
728
Steve Naroff218bc2b2007-05-04 21:54:46 +0000729inline QualType Sema::CheckEqualityOperands( // C99 6.5.9
730 Expr *lex, Expr *rex, SourceLocation loc)
Steve Naroff1926c832007-04-24 00:23:05 +0000731{
732 QualType lType = lex->getType(), rType = rex->getType();
733
734 if (lType->isArithmeticType() && rType->isArithmeticType())
Steve Naroff218bc2b2007-05-04 21:54:46 +0000735 return Context.IntTy;
Steve Naroffe4718892007-04-27 18:30:00 +0000736 if (lType->isPointerType() && rType->isPointerType())
Steve Naroff218bc2b2007-05-04 21:54:46 +0000737 return Context.IntTy;
738
Steve Naroff043d45d2007-05-15 02:32:35 +0000739 if (lType->isIntegerType() || rType->isIntegerType()) { // GCC extension.
Steve Naroff218bc2b2007-05-04 21:54:46 +0000740 Diag(loc, diag::ext_typecheck_comparison_of_pointer_integer);
Steve Naroff043d45d2007-05-15 02:32:35 +0000741 return Context.IntTy;
742 }
Steve Naroffe845e272007-05-18 01:06:45 +0000743 Diag(loc, diag::err_typecheck_invalid_operands,
Chris Lattner84e160a2007-05-19 07:03:17 +0000744 lType.getAsString(), rType.getAsString(),
745 lex->getSourceRange(), rex->getSourceRange());
Steve Naroff218bc2b2007-05-04 21:54:46 +0000746 return QualType();
Steve Naroff26c8ea52007-03-21 21:08:52 +0000747}
748
Steve Naroff218bc2b2007-05-04 21:54:46 +0000749inline QualType Sema::CheckBitwiseOperands(
750 Expr *lex, Expr *rex, SourceLocation loc)
Steve Naroff1926c832007-04-24 00:23:05 +0000751{
752 QualType resType = UsualArithmeticConversions(lex->getType(), rex->getType());
753
Steve Naroff218bc2b2007-05-04 21:54:46 +0000754 if (resType->isIntegerType())
755 return resType;
Steve Naroffe845e272007-05-18 01:06:45 +0000756 Diag(loc, diag::err_typecheck_invalid_operands,
Chris Lattner84e160a2007-05-19 07:03:17 +0000757 lex->getType().getAsString(), rex->getType().getAsString(),
758 lex->getSourceRange(), rex->getSourceRange());
Steve Naroff218bc2b2007-05-04 21:54:46 +0000759 return QualType();
Steve Naroff26c8ea52007-03-21 21:08:52 +0000760}
761
Steve Naroff218bc2b2007-05-04 21:54:46 +0000762inline QualType Sema::CheckLogicalOperands( // C99 6.5.[13,14]
763 Expr *lex, Expr *rex, SourceLocation loc)
Steve Naroff1926c832007-04-24 00:23:05 +0000764{
Steve Naroffe4718892007-04-27 18:30:00 +0000765 QualType lhsType = UsualUnaryConversion(lex->getType());
766 QualType rhsType = UsualUnaryConversion(rex->getType());
767
Steve Naroff218bc2b2007-05-04 21:54:46 +0000768 if (lhsType->isScalarType() || rhsType->isScalarType())
769 return Context.IntTy;
Steve Naroffe845e272007-05-18 01:06:45 +0000770 Diag(loc, diag::err_typecheck_invalid_operands,
Chris Lattner84e160a2007-05-19 07:03:17 +0000771 lex->getType().getAsString(), rex->getType().getAsString(),
772 lex->getSourceRange(), rex->getSourceRange());
Steve Naroff218bc2b2007-05-04 21:54:46 +0000773 return QualType();
Steve Naroffae4143e2007-04-26 20:39:23 +0000774}
775
Steve Naroff35d85152007-05-07 00:24:15 +0000776inline QualType Sema::CheckAssignmentOperands( // C99 6.5.16.1
777 Expr *lex, Expr *rex, SourceLocation loc, QualType compoundType)
Steve Naroffae4143e2007-04-26 20:39:23 +0000778{
Steve Naroff0af91202007-04-27 21:51:21 +0000779 QualType lhsType = lex->getType();
Steve Naroff35d85152007-05-07 00:24:15 +0000780 QualType rhsType = compoundType.isNull() ? rex->getType() : compoundType;
Steve Naroff1f4d7272007-05-11 04:00:31 +0000781 bool hadError = false;
Steve Naroff218bc2b2007-05-04 21:54:46 +0000782
783 // this check is done first to give a more precise diagnostic.
Steve Naroff1f4d7272007-05-11 04:00:31 +0000784 // isModifiableLvalue() will also check for "const".
Steve Naroff218bc2b2007-05-04 21:54:46 +0000785 if (lhsType.isConstQualified()) {
Steve Naroff71ce2e02007-05-18 22:53:50 +0000786 Diag(loc, diag::err_typecheck_assign_const, lex->getSourceRange());
Steve Naroff1f4d7272007-05-11 04:00:31 +0000787 hadError = true;
788 } else if (!lex->isModifiableLvalue()) { // C99 6.5.16p2
Steve Naroff71ce2e02007-05-18 22:53:50 +0000789 Diag(loc, diag::err_typecheck_assign_non_lvalue, lex->getSourceRange());
Steve Naroff1f4d7272007-05-11 04:00:31 +0000790 return QualType(); // no need to continue checking...
Steve Naroff218bc2b2007-05-04 21:54:46 +0000791 }
792 if (lhsType == rhsType) // common case, fast path...
793 return lhsType;
794
795 AssignmentConversionResult result;
796 QualType resType = UsualAssignmentConversions(lhsType, rhsType, result);
797
798 // decode the result (notice that extensions still return a type).
799 switch (result) {
800 case Compatible:
Steve Naroff1f4d7272007-05-11 04:00:31 +0000801 break;
Steve Naroff218bc2b2007-05-04 21:54:46 +0000802 case Incompatible:
Steve Naroffe845e272007-05-18 01:06:45 +0000803 Diag(loc, diag::err_typecheck_assign_incompatible,
Chris Lattner84e160a2007-05-19 07:03:17 +0000804 lhsType.getAsString(), rhsType.getAsString(),
805 lex->getSourceRange(), rex->getSourceRange());
Steve Naroff1f4d7272007-05-11 04:00:31 +0000806 hadError = true;
807 break;
Steve Naroff218bc2b2007-05-04 21:54:46 +0000808 case PointerFromInt:
809 // check for null pointer constant (C99 6.3.2.3p3)
Steve Naroff35d85152007-05-07 00:24:15 +0000810 if (compoundType.isNull() && !rex->isNullPointerConstant())
Steve Naroff218bc2b2007-05-04 21:54:46 +0000811 Diag(loc, diag::ext_typecheck_assign_pointer_from_int);
Steve Naroff1f4d7272007-05-11 04:00:31 +0000812 break;
Steve Naroff218bc2b2007-05-04 21:54:46 +0000813 case IntFromPointer:
814 Diag(loc, diag::ext_typecheck_assign_int_from_pointer);
Steve Naroff1f4d7272007-05-11 04:00:31 +0000815 break;
Steve Naroff218bc2b2007-05-04 21:54:46 +0000816 case IncompatiblePointer:
817 Diag(loc, diag::ext_typecheck_assign_incompatible_pointer);
Steve Naroff1f4d7272007-05-11 04:00:31 +0000818 break;
819 case CompatiblePointerDiscardsQualifiers:
820 Diag(loc, diag::ext_typecheck_assign_discards_qualifiers);
821 break;
Steve Naroff218bc2b2007-05-04 21:54:46 +0000822 }
Steve Naroff1f4d7272007-05-11 04:00:31 +0000823 return hadError ? QualType() : resType;
Steve Naroffae4143e2007-04-26 20:39:23 +0000824}
825
Steve Naroff218bc2b2007-05-04 21:54:46 +0000826inline QualType Sema::CheckCommaOperands( // C99 6.5.17
Chris Lattner84e160a2007-05-19 07:03:17 +0000827 Expr *lex, Expr *rex, SourceLocation loc) {
Steve Naroff218bc2b2007-05-04 21:54:46 +0000828 return UsualUnaryConversion(rex->getType());
Steve Naroff95af0132007-03-30 23:47:58 +0000829}
830
Steve Naroff71ce2e02007-05-18 22:53:50 +0000831QualType Sema::CheckIncrementDecrementOperand(Expr *op, SourceLocation OpLoc) {
Steve Naroff094046f2007-05-13 03:21:25 +0000832 QualType resType = UsualArithmeticConversions(op->getType(), Context.IntTy);
Steve Naroff35d85152007-05-07 00:24:15 +0000833 assert(!resType.isNull() && "no type for increment/decrement expression");
Steve Naroffd50c88e2007-04-05 21:15:20 +0000834
Steve Naroff46ba1eb2007-04-03 23:13:13 +0000835 // C99 6.5.2.4p1
Steve Naroff35d85152007-05-07 00:24:15 +0000836 if (const PointerType *pt = dyn_cast<PointerType>(resType)) {
837 if (!pt->getPointeeType()->isObjectType()) { // C99 6.5.2.4p2, 6.5.6p2
Steve Naroff71ce2e02007-05-18 22:53:50 +0000838 Diag(OpLoc, diag::err_typecheck_arithmetic_incomplete_type,
839 resType.getAsString(), op->getSourceRange());
Steve Naroff35d85152007-05-07 00:24:15 +0000840 return QualType();
841 }
842 } else if (!resType->isRealType()) {
Steve Naroff95af0132007-03-30 23:47:58 +0000843 // FIXME: Allow Complex as a GCC extension.
Steve Naroff71ce2e02007-05-18 22:53:50 +0000844 Diag(OpLoc, diag::err_typecheck_illegal_increment_decrement,
845 resType.getAsString(), op->getSourceRange());
Steve Naroff35d85152007-05-07 00:24:15 +0000846 return QualType();
Steve Naroff46ba1eb2007-04-03 23:13:13 +0000847 }
Steve Naroff094046f2007-05-13 03:21:25 +0000848 // At this point, we know we have a real or pointer type. Now make sure
849 // the operand is a modifiable lvalue.
850 if (!op->isModifiableLvalue()) {
Steve Naroff71ce2e02007-05-18 22:53:50 +0000851 Diag(OpLoc, diag::err_typecheck_invalid_lvalue_incr_decr,
Chris Lattner84e160a2007-05-19 07:03:17 +0000852 op->getSourceRange());
Steve Naroff35d85152007-05-07 00:24:15 +0000853 return QualType();
854 }
855 return resType;
Steve Naroff26c8ea52007-03-21 21:08:52 +0000856}
857
Steve Naroff1926c832007-04-24 00:23:05 +0000858/// getPrimaryDeclaration - Helper function for CheckAddressOfOperand().
Steve Naroff47500512007-04-19 23:00:49 +0000859/// This routine allows us to typecheck complex/recursive expressions
860/// where the declaration is needed for type checking. Here are some
861/// examples: &s.xx, &s.zz[1].yy, &(1+2), &(XX), &"123"[2].
Steve Naroff1926c832007-04-24 00:23:05 +0000862static Decl *getPrimaryDeclaration(Expr *e) {
Steve Naroff47500512007-04-19 23:00:49 +0000863 switch (e->getStmtClass()) {
864 case Stmt::DeclRefExprClass:
865 return cast<DeclRefExpr>(e)->getDecl();
866 case Stmt::MemberExprClass:
Steve Naroff1926c832007-04-24 00:23:05 +0000867 return getPrimaryDeclaration(cast<MemberExpr>(e)->getBase());
Steve Naroff47500512007-04-19 23:00:49 +0000868 case Stmt::ArraySubscriptExprClass:
Steve Naroff1926c832007-04-24 00:23:05 +0000869 return getPrimaryDeclaration(cast<ArraySubscriptExpr>(e)->getBase());
Steve Naroff47500512007-04-19 23:00:49 +0000870 case Stmt::CallExprClass:
Steve Naroff1926c832007-04-24 00:23:05 +0000871 return getPrimaryDeclaration(cast<CallExpr>(e)->getCallee());
Steve Naroff47500512007-04-19 23:00:49 +0000872 case Stmt::UnaryOperatorClass:
Steve Naroff1926c832007-04-24 00:23:05 +0000873 return getPrimaryDeclaration(cast<UnaryOperator>(e)->getSubExpr());
Steve Naroff47500512007-04-19 23:00:49 +0000874 case Stmt::ParenExprClass:
Steve Naroff1926c832007-04-24 00:23:05 +0000875 return getPrimaryDeclaration(cast<ParenExpr>(e)->getSubExpr());
Steve Naroff47500512007-04-19 23:00:49 +0000876 default:
877 return 0;
878 }
879}
880
881/// CheckAddressOfOperand - The operand of & must be either a function
882/// designator or an lvalue designating an object. If it is an lvalue, the
883/// object cannot be declared with storage class register or be a bit field.
884/// Note: The usual conversions are *not* applied to the operand of the &
Steve Naroffa78fe7e2007-05-16 19:47:19 +0000885/// operator (C99 6.3.2.1p[2-4]), and its result is never an lvalue.
Steve Naroff35d85152007-05-07 00:24:15 +0000886QualType Sema::CheckAddressOfOperand(Expr *op, SourceLocation OpLoc) {
Steve Naroff1926c832007-04-24 00:23:05 +0000887 Decl *dcl = getPrimaryDeclaration(op);
Steve Naroff47500512007-04-19 23:00:49 +0000888
Steve Naroff5dd642e2007-05-14 18:14:51 +0000889 if (!op->isLvalue()) { // C99 6.5.3.2p1
890 if (dcl && isa<FunctionDecl>(dcl)) // allow function designators
891 ;
Steve Naroff35d85152007-05-07 00:24:15 +0000892 else {
Steve Naroff71ce2e02007-05-18 22:53:50 +0000893 Diag(OpLoc, diag::err_typecheck_invalid_lvalue_addrof,
894 op->getSourceRange());
Steve Naroff35d85152007-05-07 00:24:15 +0000895 return QualType();
896 }
Steve Naroff47500512007-04-19 23:00:49 +0000897 } else if (dcl) {
898 // We have an lvalue with a decl. Make sure the decl is not declared
899 // with the register storage-class specifier.
900 if (const VarDecl *vd = dyn_cast<VarDecl>(dcl)) {
Steve Naroff35d85152007-05-07 00:24:15 +0000901 if (vd->getStorageClass() == VarDecl::Register) {
Steve Naroff71ce2e02007-05-18 22:53:50 +0000902 Diag(OpLoc, diag::err_typecheck_address_of_register,
Chris Lattner84e160a2007-05-19 07:03:17 +0000903 op->getSourceRange());
Steve Naroff35d85152007-05-07 00:24:15 +0000904 return QualType();
905 }
Steve Narofff633d092007-04-25 19:01:39 +0000906 } else
907 assert(0 && "Unknown/unexpected decl type");
908
Steve Naroff47500512007-04-19 23:00:49 +0000909 // FIXME: add check for bitfields!
910 }
911 // If the operand has type "type", the result has type "pointer to type".
Steve Naroff35d85152007-05-07 00:24:15 +0000912 return Context.getPointerType(op->getType());
Steve Naroff47500512007-04-19 23:00:49 +0000913}
914
Steve Naroff35d85152007-05-07 00:24:15 +0000915QualType Sema::CheckIndirectionOperand(Expr *op, SourceLocation OpLoc) {
916 QualType qType = UsualUnaryConversion(op->getType());
917
Steve Naroff47500512007-04-19 23:00:49 +0000918 assert(!qType.isNull() && "no type for * expression");
919
Steve Naroff35d85152007-05-07 00:24:15 +0000920 if (PointerType *PT = dyn_cast<PointerType>(qType))
921 return PT->getPointeeType();
Steve Naroff71ce2e02007-05-18 22:53:50 +0000922 Diag(OpLoc, diag::err_typecheck_unary_expr, qType.getAsString(),
Chris Lattner84e160a2007-05-19 07:03:17 +0000923 op->getSourceRange());
Steve Naroff35d85152007-05-07 00:24:15 +0000924 return QualType();
Steve Naroff1926c832007-04-24 00:23:05 +0000925}
Steve Naroff218bc2b2007-05-04 21:54:46 +0000926
927static inline BinaryOperator::Opcode ConvertTokenKindToBinaryOpcode(
928 tok::TokenKind Kind) {
929 BinaryOperator::Opcode Opc;
930 switch (Kind) {
931 default: assert(0 && "Unknown binop!");
932 case tok::star: Opc = BinaryOperator::Mul; break;
933 case tok::slash: Opc = BinaryOperator::Div; break;
934 case tok::percent: Opc = BinaryOperator::Rem; break;
935 case tok::plus: Opc = BinaryOperator::Add; break;
936 case tok::minus: Opc = BinaryOperator::Sub; break;
937 case tok::lessless: Opc = BinaryOperator::Shl; break;
938 case tok::greatergreater: Opc = BinaryOperator::Shr; break;
939 case tok::lessequal: Opc = BinaryOperator::LE; break;
940 case tok::less: Opc = BinaryOperator::LT; break;
941 case tok::greaterequal: Opc = BinaryOperator::GE; break;
942 case tok::greater: Opc = BinaryOperator::GT; break;
943 case tok::exclaimequal: Opc = BinaryOperator::NE; break;
944 case tok::equalequal: Opc = BinaryOperator::EQ; break;
945 case tok::amp: Opc = BinaryOperator::And; break;
946 case tok::caret: Opc = BinaryOperator::Xor; break;
947 case tok::pipe: Opc = BinaryOperator::Or; break;
948 case tok::ampamp: Opc = BinaryOperator::LAnd; break;
949 case tok::pipepipe: Opc = BinaryOperator::LOr; break;
950 case tok::equal: Opc = BinaryOperator::Assign; break;
951 case tok::starequal: Opc = BinaryOperator::MulAssign; break;
952 case tok::slashequal: Opc = BinaryOperator::DivAssign; break;
953 case tok::percentequal: Opc = BinaryOperator::RemAssign; break;
954 case tok::plusequal: Opc = BinaryOperator::AddAssign; break;
955 case tok::minusequal: Opc = BinaryOperator::SubAssign; break;
956 case tok::lesslessequal: Opc = BinaryOperator::ShlAssign; break;
957 case tok::greatergreaterequal: Opc = BinaryOperator::ShrAssign; break;
958 case tok::ampequal: Opc = BinaryOperator::AndAssign; break;
959 case tok::caretequal: Opc = BinaryOperator::XorAssign; break;
960 case tok::pipeequal: Opc = BinaryOperator::OrAssign; break;
961 case tok::comma: Opc = BinaryOperator::Comma; break;
962 }
963 return Opc;
964}
965
Steve Naroff35d85152007-05-07 00:24:15 +0000966static inline UnaryOperator::Opcode ConvertTokenKindToUnaryOpcode(
967 tok::TokenKind Kind) {
968 UnaryOperator::Opcode Opc;
969 switch (Kind) {
970 default: assert(0 && "Unknown unary op!");
971 case tok::plusplus: Opc = UnaryOperator::PreInc; break;
972 case tok::minusminus: Opc = UnaryOperator::PreDec; break;
973 case tok::amp: Opc = UnaryOperator::AddrOf; break;
974 case tok::star: Opc = UnaryOperator::Deref; break;
975 case tok::plus: Opc = UnaryOperator::Plus; break;
976 case tok::minus: Opc = UnaryOperator::Minus; break;
977 case tok::tilde: Opc = UnaryOperator::Not; break;
978 case tok::exclaim: Opc = UnaryOperator::LNot; break;
979 case tok::kw_sizeof: Opc = UnaryOperator::SizeOf; break;
980 case tok::kw___alignof: Opc = UnaryOperator::AlignOf; break;
981 case tok::kw___real: Opc = UnaryOperator::Real; break;
982 case tok::kw___imag: Opc = UnaryOperator::Imag; break;
983 case tok::ampamp: Opc = UnaryOperator::AddrLabel; break;
984 // FIXME: case tok::kw___extension__:
985 }
986 return Opc;
987}
988
Steve Naroff218bc2b2007-05-04 21:54:46 +0000989// Binary Operators. 'Tok' is the token for the operator.
990Action::ExprResult Sema::ParseBinOp(SourceLocation TokLoc, tok::TokenKind Kind,
991 ExprTy *LHS, ExprTy *RHS) {
992 BinaryOperator::Opcode Opc = ConvertTokenKindToBinaryOpcode(Kind);
993 Expr *lhs = (Expr *)LHS, *rhs = (Expr*)RHS;
994
995 assert((lhs != 0) && "ParseBinOp(): missing left expression");
996 assert((rhs != 0) && "ParseBinOp(): missing right expression");
997
998 QualType result;
999
1000 switch (Opc) {
1001 default:
1002 assert(0 && "Unknown binary expr!");
1003 case BinaryOperator::Assign:
Steve Naroff35d85152007-05-07 00:24:15 +00001004 result = CheckAssignmentOperands(lhs, rhs, TokLoc, QualType());
Steve Naroff218bc2b2007-05-04 21:54:46 +00001005 break;
1006 case BinaryOperator::Mul:
1007 case BinaryOperator::Div:
1008 result = CheckMultiplyDivideOperands(lhs, rhs, TokLoc);
1009 break;
1010 case BinaryOperator::Rem:
1011 result = CheckRemainderOperands(lhs, rhs, TokLoc);
1012 break;
1013 case BinaryOperator::Add:
1014 result = CheckAdditionOperands(lhs, rhs, TokLoc);
1015 break;
1016 case BinaryOperator::Sub:
1017 result = CheckSubtractionOperands(lhs, rhs, TokLoc);
1018 break;
1019 case BinaryOperator::Shl:
1020 case BinaryOperator::Shr:
1021 result = CheckShiftOperands(lhs, rhs, TokLoc);
1022 break;
1023 case BinaryOperator::LE:
1024 case BinaryOperator::LT:
1025 case BinaryOperator::GE:
1026 case BinaryOperator::GT:
1027 result = CheckRelationalOperands(lhs, rhs, TokLoc);
1028 break;
1029 case BinaryOperator::EQ:
1030 case BinaryOperator::NE:
1031 result = CheckEqualityOperands(lhs, rhs, TokLoc);
1032 break;
1033 case BinaryOperator::And:
1034 case BinaryOperator::Xor:
1035 case BinaryOperator::Or:
1036 result = CheckBitwiseOperands(lhs, rhs, TokLoc);
1037 break;
1038 case BinaryOperator::LAnd:
1039 case BinaryOperator::LOr:
1040 result = CheckLogicalOperands(lhs, rhs, TokLoc);
1041 break;
1042 case BinaryOperator::MulAssign:
1043 case BinaryOperator::DivAssign:
1044 result = CheckMultiplyDivideOperands(lhs, rhs, TokLoc);
1045 if (result.isNull())
1046 return true;
Steve Naroff35d85152007-05-07 00:24:15 +00001047 result = CheckAssignmentOperands(lhs, rhs, TokLoc, result);
Steve Naroff218bc2b2007-05-04 21:54:46 +00001048 break;
1049 case BinaryOperator::RemAssign:
1050 result = CheckRemainderOperands(lhs, rhs, TokLoc);
1051 if (result.isNull())
1052 return true;
Steve Naroff35d85152007-05-07 00:24:15 +00001053 result = CheckAssignmentOperands(lhs, rhs, TokLoc, result);
Steve Naroff218bc2b2007-05-04 21:54:46 +00001054 break;
1055 case BinaryOperator::AddAssign:
1056 result = CheckAdditionOperands(lhs, rhs, TokLoc);
1057 if (result.isNull())
1058 return true;
Steve Naroff35d85152007-05-07 00:24:15 +00001059 result = CheckAssignmentOperands(lhs, rhs, TokLoc, result);
Steve Naroff218bc2b2007-05-04 21:54:46 +00001060 break;
1061 case BinaryOperator::SubAssign:
1062 result = CheckSubtractionOperands(lhs, rhs, TokLoc);
1063 if (result.isNull())
1064 return true;
Steve Naroff35d85152007-05-07 00:24:15 +00001065 result = CheckAssignmentOperands(lhs, rhs, TokLoc, result);
Steve Naroff218bc2b2007-05-04 21:54:46 +00001066 break;
1067 case BinaryOperator::ShlAssign:
1068 case BinaryOperator::ShrAssign:
1069 result = CheckShiftOperands(lhs, rhs, TokLoc);
1070 if (result.isNull())
1071 return true;
Steve Naroff35d85152007-05-07 00:24:15 +00001072 result = CheckAssignmentOperands(lhs, rhs, TokLoc, result);
Steve Naroff218bc2b2007-05-04 21:54:46 +00001073 break;
1074 case BinaryOperator::AndAssign:
1075 case BinaryOperator::XorAssign:
1076 case BinaryOperator::OrAssign:
1077 result = CheckBitwiseOperands(lhs, rhs, TokLoc);
1078 if (result.isNull())
1079 return true;
Steve Naroff35d85152007-05-07 00:24:15 +00001080 result = CheckAssignmentOperands(lhs, rhs, TokLoc, result);
Steve Naroff218bc2b2007-05-04 21:54:46 +00001081 break;
1082 case BinaryOperator::Comma:
1083 result = CheckCommaOperands(lhs, rhs, TokLoc);
1084 break;
1085 }
1086 if (result.isNull())
1087 return true;
1088 return new BinaryOperator(lhs, rhs, Opc, result);
1089}
1090
Steve Naroff35d85152007-05-07 00:24:15 +00001091// Unary Operators. 'Tok' is the token for the operator.
1092Action::ExprResult Sema::ParseUnaryOp(SourceLocation OpLoc, tok::TokenKind Op,
1093 ExprTy *Input) {
1094 UnaryOperator::Opcode Opc = ConvertTokenKindToUnaryOpcode(Op);
1095 QualType resultType;
1096 switch (Opc) {
1097 default:
1098 assert(0 && "Unimplemented unary expr!");
1099 case UnaryOperator::PreInc:
1100 case UnaryOperator::PreDec:
Steve Naroff71ce2e02007-05-18 22:53:50 +00001101 resultType = CheckIncrementDecrementOperand((Expr *)Input, OpLoc);
Steve Naroff35d85152007-05-07 00:24:15 +00001102 break;
1103 case UnaryOperator::AddrOf:
1104 resultType = CheckAddressOfOperand((Expr *)Input, OpLoc);
1105 break;
1106 case UnaryOperator::Deref:
1107 resultType = CheckIndirectionOperand((Expr *)Input, OpLoc);
1108 break;
1109 case UnaryOperator::Plus:
1110 case UnaryOperator::Minus:
1111 resultType = UsualUnaryConversion(((Expr *)Input)->getType());
1112 if (!resultType->isArithmeticType()) // C99 6.5.3.3p1
Chris Lattnerc04bd6a2007-05-16 18:09:54 +00001113 return Diag(OpLoc, diag::err_typecheck_unary_expr,
1114 resultType.getAsString());
Steve Naroff35d85152007-05-07 00:24:15 +00001115 break;
1116 case UnaryOperator::Not: // bitwise complement
1117 resultType = UsualUnaryConversion(((Expr *)Input)->getType());
1118 if (!resultType->isIntegerType()) // C99 6.5.3.3p1
Chris Lattnerc04bd6a2007-05-16 18:09:54 +00001119 return Diag(OpLoc, diag::err_typecheck_unary_expr,
1120 resultType.getAsString());
Steve Naroff35d85152007-05-07 00:24:15 +00001121 break;
1122 case UnaryOperator::LNot: // logical negation
1123 resultType = UsualUnaryConversion(((Expr *)Input)->getType());
1124 if (!resultType->isScalarType()) // C99 6.5.3.3p1
Chris Lattnerc04bd6a2007-05-16 18:09:54 +00001125 return Diag(OpLoc, diag::err_typecheck_unary_expr,
1126 resultType.getAsString());
Steve Naroff35d85152007-05-07 00:24:15 +00001127 break;
1128 case UnaryOperator::SizeOf:
Steve Naroff043d45d2007-05-15 02:32:35 +00001129 resultType = CheckSizeOfAlignOfOperand(((Expr *)Input)->getType(), OpLoc,
1130 true);
1131 break;
Steve Naroff35d85152007-05-07 00:24:15 +00001132 case UnaryOperator::AlignOf:
Steve Naroff043d45d2007-05-15 02:32:35 +00001133 resultType = CheckSizeOfAlignOfOperand(((Expr *)Input)->getType(), OpLoc,
1134 false);
1135 break;
Steve Naroff35d85152007-05-07 00:24:15 +00001136 }
1137 if (resultType.isNull())
1138 return true;
Steve Naroff509fe022007-05-17 01:16:00 +00001139 return new UnaryOperator((Expr *)Input, Opc, resultType, OpLoc);
Steve Naroff35d85152007-05-07 00:24:15 +00001140}