blob: 0631a87b1fe673090ef8b6651c06067e73c577f4 [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 }
424 if (lhs->isPointerType() && rhs->isPointerType()) { // C99 6.5.15p3,6
425 QualType lhptee, rhptee;
426
427 // get the "pointed to" type
428 lhptee = cast<PointerType>(lhs.getCanonicalType())->getPointeeType();
429 rhptee = cast<PointerType>(rhs.getCanonicalType())->getPointeeType();
430
431 // ignore qualifiers on void (C99 6.5.15p3, clause 6)
432 if (lhptee.getUnqualifiedType()->isVoidType() &&
433 (rhptee->isObjectType() || rhptee->isIncompleteType()))
434 return lhs;
435 if (rhptee.getUnqualifiedType()->isVoidType() &&
436 (lhptee->isObjectType() || lhptee->isIncompleteType()))
437 return rhs;
438
439 // FIXME: C99 6.5.15p6: If both operands are pointers to compatible types
440 // *or* to differently qualified versions of compatible types, the result
441 // type is a pointer to an appropriately qualified version of the
442 // *composite* type.
443 if (!Type::typesAreCompatible(lhptee.getUnqualifiedType(),
444 rhptee.getUnqualifiedType())) {
445 Diag(questionLoc, diag::ext_typecheck_cond_incompatible_pointers,
446 lhs.getAsString(), rhs.getAsString());
447 return lhs; // FIXME: this is an _ext - is this return o.k?
448 }
449 }
450 if (lhs->isVoidType() && rhs->isVoidType()) // C99 6.5.15p3
451 return lhs;
452 if (lhs->isPointerType() && RHS->isNullPointerConstant()) // C99 6.5.15p3
453 return lhs;
454 if (rhs->isPointerType() && LHS->isNullPointerConstant()) // C99 6.5.15p3
455 return rhs;
456
457 Diag(questionLoc, diag::err_typecheck_cond_incompatible_operands,
458 lhs.getAsString(), rhs.getAsString());
459 return QualType();
Steve Narofff8a28c52007-05-15 20:29:32 +0000460}
461
Chris Lattnere168f762006-11-10 05:29:30 +0000462/// ParseConditionalOp - Parse a ?: operation. Note that 'LHS' may be null
463/// in the case of a the GNU conditional expr extension.
464Action::ExprResult Sema::ParseConditionalOp(SourceLocation QuestionLoc,
465 SourceLocation ColonLoc,
466 ExprTy *Cond, ExprTy *LHS,
467 ExprTy *RHS) {
Steve Narofff8a28c52007-05-15 20:29:32 +0000468 QualType result = CheckConditionalOperands((Expr *)Cond, (Expr *)LHS,
469 (Expr *)RHS, QuestionLoc);
470 if (result.isNull())
471 return true;
472 return new ConditionalOperator((Expr*)Cond, (Expr*)LHS, (Expr*)RHS, result);
Chris Lattnere168f762006-11-10 05:29:30 +0000473}
474
Steve Naroff1926c832007-04-24 00:23:05 +0000475/// UsualUnaryConversion - Performs various conversions that are common to most
476/// operators (C99 6.3). The conversions of array and function types are
477/// sometimes surpressed. For example, the array->pointer conversion doesn't
478/// apply if the array is an argument to the sizeof or address (&) operators.
479/// In these instances, this routine should *not* be called.
480QualType Sema::UsualUnaryConversion(QualType t) {
481 assert(!t.isNull() && "UsualUnaryConversion - missing type");
Steve Naroff4b7ce032007-04-20 22:26:17 +0000482
Steve Naroff1926c832007-04-24 00:23:05 +0000483 if (t->isPromotableIntegerType()) // C99 6.3.1.1p2
484 return Context.IntTy;
Steve Naroffa78fe7e2007-05-16 19:47:19 +0000485 if (t->isFunctionType()) // C99 6.3.2.1p4
486 return Context.getPointerType(t.getCanonicalType());
487 if (const ArrayType *ary = dyn_cast<ArrayType>(t.getCanonicalType()))
488 return Context.getPointerType(ary->getElementType()); // C99 6.3.2.1p3
Steve Naroff1926c832007-04-24 00:23:05 +0000489 return t;
490}
491
492/// UsualArithmeticConversions - Performs various conversions that are common to
493/// binary operators (C99 6.3.1.8). If both operands aren't arithmetic, this
494/// routine returns the first non-arithmetic type found. The client is
495/// responsible for emitting appropriate error diagnostics.
496QualType Sema::UsualArithmeticConversions(QualType t1, QualType t2) {
Steve Naroffb01bbe32007-04-25 01:22:31 +0000497 QualType lhs = UsualUnaryConversion(t1);
498 QualType rhs = UsualUnaryConversion(t2);
Steve Naroff1926c832007-04-24 00:23:05 +0000499
500 // if either operand is not of arithmetic type, no conversion is possible.
Steve Naroffb01bbe32007-04-25 01:22:31 +0000501 if (!lhs->isArithmeticType())
502 return lhs;
Steve Narofff633d092007-04-25 19:01:39 +0000503 if (!rhs->isArithmeticType())
Steve Naroffb01bbe32007-04-25 01:22:31 +0000504 return rhs;
Steve Naroff1926c832007-04-24 00:23:05 +0000505
Steve Narofff633d092007-04-25 19:01:39 +0000506 // if both arithmetic types are identical, no conversion is needed.
Steve Naroffb01bbe32007-04-25 01:22:31 +0000507 if (lhs == rhs)
508 return lhs;
Steve Naroff1926c832007-04-24 00:23:05 +0000509
Steve Naroffb01bbe32007-04-25 01:22:31 +0000510 // at this point, we have two different arithmetic types.
511
512 // Handle complex types first (C99 6.3.1.8p1).
513 if (lhs->isComplexType() || rhs->isComplexType()) {
514 // if we have an integer operand, the result is the complex type.
515 if (rhs->isIntegerType())
516 return lhs;
517 if (lhs->isIntegerType())
518 return rhs;
519
Steve Naroffe4718892007-04-27 18:30:00 +0000520 return Context.maxComplexType(lhs, rhs);
Steve Naroffbf223ba2007-04-24 20:56:26 +0000521 }
Steve Naroffb01bbe32007-04-25 01:22:31 +0000522 // Now handle "real" floating types (i.e. float, double, long double).
523 if (lhs->isRealFloatingType() || rhs->isRealFloatingType()) {
524 // if we have an integer operand, the result is the real floating type.
525 if (rhs->isIntegerType())
526 return lhs;
527 if (lhs->isIntegerType())
528 return rhs;
529
530 // we have two real floating types, float/complex combos were handled above.
Steve Naroffe4718892007-04-27 18:30:00 +0000531 return Context.maxFloatingType(lhs, rhs);
Steve Naroffb01bbe32007-04-25 01:22:31 +0000532 }
Steve Naroffe4718892007-04-27 18:30:00 +0000533 return Context.maxIntegerType(lhs, rhs);
Steve Narofff1e53692007-03-23 22:27:02 +0000534}
535
Steve Naroff3f597292007-05-11 22:18:03 +0000536// CheckPointerTypesForAssignment - This is a very tricky routine (despite
537// being closely modeled after the C99 spec:-). The odd characteristic of this
538// routine is it effectively iqnores the qualifiers on the top level pointee.
539// This circumvents the usual type rules specified in 6.2.7p1 & 6.7.5.[1-3].
540// FIXME: add a couple examples in this comment.
541QualType Sema::CheckPointerTypesForAssignment(QualType lhsType,
542 QualType rhsType,
543 AssignmentConversionResult &r) {
544 QualType lhptee, rhptee;
Steve Naroff1f4d7272007-05-11 04:00:31 +0000545
546 // get the "pointed to" type (ignoring qualifiers at the top level)
Steve Naroff3f597292007-05-11 22:18:03 +0000547 lhptee = cast<PointerType>(lhsType.getCanonicalType())->getPointeeType();
548 rhptee = cast<PointerType>(rhsType.getCanonicalType())->getPointeeType();
Steve Naroff1f4d7272007-05-11 04:00:31 +0000549
550 // make sure we operate on the canonical type
Steve Naroff3f597292007-05-11 22:18:03 +0000551 lhptee = lhptee.getCanonicalType();
552 rhptee = rhptee.getCanonicalType();
Steve Naroff1f4d7272007-05-11 04:00:31 +0000553
Steve Naroff3f597292007-05-11 22:18:03 +0000554 // C99 6.5.16.1p1: This following citation is common to constraints
555 // 3 & 4 (below). ...and the type *pointed to* by the left has all the
556 // qualifiers of the type *pointed to* by the right;
557 if ((lhptee.getQualifiers() & rhptee.getQualifiers()) !=
558 rhptee.getQualifiers())
559 r = CompatiblePointerDiscardsQualifiers;
560
561 // C99 6.5.16.1p1 (constraint 4): If one operand is a pointer to an object or
562 // incomplete type and the other is a pointer to a qualified or unqualified
563 // version of void...
564 if (lhptee.getUnqualifiedType()->isVoidType() &&
565 (rhptee->isObjectType() || rhptee->isIncompleteType()))
566 ;
567 else if (rhptee.getUnqualifiedType()->isVoidType() &&
568 (lhptee->isObjectType() || lhptee->isIncompleteType()))
569 ;
570 // C99 6.5.16.1p1 (constraint 3): both operands are pointers to qualified or
571 // unqualified versions of compatible types, ...
572 else if (!Type::typesAreCompatible(lhptee.getUnqualifiedType(),
573 rhptee.getUnqualifiedType()))
574 r = IncompatiblePointer; // this "trumps" PointerAssignDiscardsQualifiers
575 return rhsType;
Steve Naroff1f4d7272007-05-11 04:00:31 +0000576}
577
Steve Naroff17f76e02007-05-03 21:03:48 +0000578/// UsualAssignmentConversions (C99 6.5.16) - This routine currently
579/// has code to accommodate several GCC extensions when type checking
580/// pointers. Here are some objectionable examples that GCC considers warnings:
581///
582/// int a, *pint;
583/// short *pshort;
584/// struct foo *pfoo;
585///
586/// pint = pshort; // warning: assignment from incompatible pointer type
587/// a = pint; // warning: assignment makes integer from pointer without a cast
588/// pint = a; // warning: assignment makes pointer from integer without a cast
589/// pint = pfoo; // warning: assignment from incompatible pointer type
590///
591/// As a result, the code for dealing with pointers is more complex than the
592/// C99 spec dictates.
593/// Note: the warning above turn into errors when -pedantic-errors is enabled.
594///
Steve Naroff218bc2b2007-05-04 21:54:46 +0000595QualType Sema::UsualAssignmentConversions(QualType lhsType, QualType rhsType,
Steve Naroff17f76e02007-05-03 21:03:48 +0000596 AssignmentConversionResult &r) {
Steve Naroffb891de32007-05-02 23:51:10 +0000597 // this check seems unnatural, however it necessary to insure the proper
598 // conversion of functions/arrays. If the conversion where done for all
599 // DeclExpr's (created by ParseIdentifierExpr), it would mess up the
600 // unary expressions that surpress this implicit conversion (&, sizeof).
601 if (rhsType->isFunctionType() || rhsType->isArrayType())
602 rhsType = UsualUnaryConversion(rhsType);
603
Steve Naroff17f76e02007-05-03 21:03:48 +0000604 r = Compatible;
Steve Naroff9eb24652007-05-02 21:58:15 +0000605 if (lhsType->isArithmeticType() && rhsType->isArithmeticType())
606 return lhsType;
607 else if (lhsType->isPointerType()) {
608 if (rhsType->isIntegerType()) {
Steve Naroff218bc2b2007-05-04 21:54:46 +0000609 r = PointerFromInt;
Steve Naroffb891de32007-05-02 23:51:10 +0000610 return rhsType;
Steve Naroff9eb24652007-05-02 21:58:15 +0000611 }
Steve Naroff3f597292007-05-11 22:18:03 +0000612 if (rhsType->isPointerType())
613 return CheckPointerTypesForAssignment(lhsType, rhsType, r);
Steve Naroff9eb24652007-05-02 21:58:15 +0000614 } else if (rhsType->isPointerType()) {
615 if (lhsType->isIntegerType()) {
616 // C99 6.5.16.1p1: the left operand is _Bool and the right is a pointer.
617 if (lhsType != Context.BoolTy)
Steve Naroff17f76e02007-05-03 21:03:48 +0000618 r = IntFromPointer;
Steve Naroffb891de32007-05-02 23:51:10 +0000619 return rhsType;
Steve Naroff9eb24652007-05-02 21:58:15 +0000620 }
Steve Naroff3f597292007-05-11 22:18:03 +0000621 if (lhsType->isPointerType())
622 return CheckPointerTypesForAssignment(lhsType, rhsType, r);
Steve Naroff1f4d7272007-05-11 04:00:31 +0000623 } else if (isa<TagType>(lhsType) && isa<TagType>(rhsType)) {
624 if (Type::tagTypesAreCompatible(lhsType, rhsType))
Steve Naroffb891de32007-05-02 23:51:10 +0000625 return rhsType;
Steve Naroff1f4d7272007-05-11 04:00:31 +0000626 }
Steve Naroff17f76e02007-05-03 21:03:48 +0000627 r = Incompatible;
628 return QualType();
Steve Naroff9eb24652007-05-02 21:58:15 +0000629}
630
Steve Naroff218bc2b2007-05-04 21:54:46 +0000631inline QualType Sema::CheckMultiplyDivideOperands(
632 Expr *lex, Expr *rex, SourceLocation loc)
Steve Naroff5c10d4b2007-04-20 23:42:24 +0000633{
Steve Naroff1926c832007-04-24 00:23:05 +0000634 QualType resType = UsualArithmeticConversions(lex->getType(), rex->getType());
Steve Naroff5c10d4b2007-04-20 23:42:24 +0000635
Steve Naroff218bc2b2007-05-04 21:54:46 +0000636 if (resType->isArithmeticType())
637 return resType;
Steve Naroffe845e272007-05-18 01:06:45 +0000638 Diag(loc, diag::err_typecheck_invalid_operands,
Chris Lattner84e160a2007-05-19 07:03:17 +0000639 lex->getType().getAsString(), rex->getType().getAsString(),
640 lex->getSourceRange(), rex->getSourceRange());
Steve Naroff218bc2b2007-05-04 21:54:46 +0000641 return QualType();
Steve Naroff26c8ea52007-03-21 21:08:52 +0000642}
643
Steve Naroff218bc2b2007-05-04 21:54:46 +0000644inline QualType Sema::CheckRemainderOperands(
645 Expr *lex, Expr *rex, SourceLocation loc)
646{
647 QualType resType = UsualArithmeticConversions(lex->getType(), rex->getType());
648
649 if (resType->isIntegerType())
650 return resType;
Steve Naroffe845e272007-05-18 01:06:45 +0000651 Diag(loc, diag::err_typecheck_invalid_operands,
Chris Lattner84e160a2007-05-19 07:03:17 +0000652 lex->getType().getAsString(), rex->getType().getAsString(),
653 lex->getSourceRange(), rex->getSourceRange());
Steve Naroff218bc2b2007-05-04 21:54:46 +0000654 return QualType();
655}
656
657inline QualType Sema::CheckAdditionOperands( // C99 6.5.6
658 Expr *lex, Expr *rex, SourceLocation loc)
Steve Naroff1926c832007-04-24 00:23:05 +0000659{
Steve Naroffe4718892007-04-27 18:30:00 +0000660 QualType lhsType = lex->getType(), rhsType = rex->getType();
661 QualType resType = UsualArithmeticConversions(lhsType, rhsType);
662
663 // handle the common case first (both operands are arithmetic).
664 if (resType->isArithmeticType())
Steve Naroff218bc2b2007-05-04 21:54:46 +0000665 return resType;
666
667 if ((lhsType->isPointerType() && rhsType->isIntegerType()) ||
668 (lhsType->isIntegerType() && rhsType->isPointerType()))
669 return resType;
Steve Naroffe845e272007-05-18 01:06:45 +0000670 Diag(loc, diag::err_typecheck_invalid_operands,
Chris Lattner84e160a2007-05-19 07:03:17 +0000671 lhsType.getAsString(), rhsType.getAsString(),
672 lex->getSourceRange(), rex->getSourceRange());
Steve Naroff218bc2b2007-05-04 21:54:46 +0000673 return QualType();
Steve Naroff26c8ea52007-03-21 21:08:52 +0000674}
675
Steve Naroff218bc2b2007-05-04 21:54:46 +0000676inline QualType Sema::CheckSubtractionOperands( // C99 6.5.6
677 Expr *lex, Expr *rex, SourceLocation loc)
678{
679 QualType lhsType = lex->getType(), rhsType = rex->getType();
680 QualType resType = UsualArithmeticConversions(lhsType, rhsType);
681
682 // handle the common case first (both operands are arithmetic).
683 if (resType->isArithmeticType())
684 return resType;
685 if ((lhsType->isPointerType() && rhsType->isIntegerType()) ||
686 (lhsType->isPointerType() && rhsType->isPointerType()))
687 return resType;
Steve Naroffe845e272007-05-18 01:06:45 +0000688 Diag(loc, diag::err_typecheck_invalid_operands,
Chris Lattner84e160a2007-05-19 07:03:17 +0000689 lhsType.getAsString(), rhsType.getAsString(),
690 lex->getSourceRange(), rex->getSourceRange());
Steve Naroff218bc2b2007-05-04 21:54:46 +0000691 return QualType();
692}
693
694inline QualType Sema::CheckShiftOperands( // C99 6.5.7
695 Expr *lex, Expr *rex, SourceLocation loc)
Steve Naroff1926c832007-04-24 00:23:05 +0000696{
697 QualType resType = UsualArithmeticConversions(lex->getType(), rex->getType());
698
Steve Naroff218bc2b2007-05-04 21:54:46 +0000699 if (resType->isIntegerType())
700 return resType;
Steve Naroffe845e272007-05-18 01:06:45 +0000701 Diag(loc, diag::err_typecheck_invalid_operands,
Chris Lattner84e160a2007-05-19 07:03:17 +0000702 lex->getType().getAsString(), rex->getType().getAsString(),
703 lex->getSourceRange(), rex->getSourceRange());
Steve Naroff218bc2b2007-05-04 21:54:46 +0000704 return QualType();
Steve Naroff26c8ea52007-03-21 21:08:52 +0000705}
706
Steve Naroff218bc2b2007-05-04 21:54:46 +0000707inline QualType Sema::CheckRelationalOperands( // C99 6.5.8
708 Expr *lex, Expr *rex, SourceLocation loc)
Steve Naroff1926c832007-04-24 00:23:05 +0000709{
710 QualType lType = lex->getType(), rType = rex->getType();
711
712 if (lType->isRealType() && rType->isRealType())
Steve Naroff218bc2b2007-05-04 21:54:46 +0000713 return Context.IntTy;
Steve Naroffe4718892007-04-27 18:30:00 +0000714
715 if (lType->isPointerType() && rType->isPointerType())
Steve Naroff218bc2b2007-05-04 21:54:46 +0000716 return Context.IntTy;
Steve Naroffe4718892007-04-27 18:30:00 +0000717
Steve Naroff043d45d2007-05-15 02:32:35 +0000718 if (lType->isIntegerType() || rType->isIntegerType()) { // GCC extension.
Steve Naroff218bc2b2007-05-04 21:54:46 +0000719 Diag(loc, diag::ext_typecheck_comparison_of_pointer_integer);
Steve Naroff043d45d2007-05-15 02:32:35 +0000720 return Context.IntTy;
721 }
Steve Naroffe845e272007-05-18 01:06:45 +0000722 Diag(loc, diag::err_typecheck_invalid_operands,
Chris Lattner84e160a2007-05-19 07:03:17 +0000723 lType.getAsString(), rType.getAsString(),
724 lex->getSourceRange(), rex->getSourceRange());
Steve Naroff218bc2b2007-05-04 21:54:46 +0000725 return QualType();
Steve Naroff26c8ea52007-03-21 21:08:52 +0000726}
727
Steve Naroff218bc2b2007-05-04 21:54:46 +0000728inline QualType Sema::CheckEqualityOperands( // C99 6.5.9
729 Expr *lex, Expr *rex, SourceLocation loc)
Steve Naroff1926c832007-04-24 00:23:05 +0000730{
731 QualType lType = lex->getType(), rType = rex->getType();
732
733 if (lType->isArithmeticType() && rType->isArithmeticType())
Steve Naroff218bc2b2007-05-04 21:54:46 +0000734 return Context.IntTy;
Steve Naroffe4718892007-04-27 18:30:00 +0000735 if (lType->isPointerType() && rType->isPointerType())
Steve Naroff218bc2b2007-05-04 21:54:46 +0000736 return Context.IntTy;
737
Steve Naroff043d45d2007-05-15 02:32:35 +0000738 if (lType->isIntegerType() || rType->isIntegerType()) { // GCC extension.
Steve Naroff218bc2b2007-05-04 21:54:46 +0000739 Diag(loc, diag::ext_typecheck_comparison_of_pointer_integer);
Steve Naroff043d45d2007-05-15 02:32:35 +0000740 return Context.IntTy;
741 }
Steve Naroffe845e272007-05-18 01:06:45 +0000742 Diag(loc, diag::err_typecheck_invalid_operands,
Chris Lattner84e160a2007-05-19 07:03:17 +0000743 lType.getAsString(), rType.getAsString(),
744 lex->getSourceRange(), rex->getSourceRange());
Steve Naroff218bc2b2007-05-04 21:54:46 +0000745 return QualType();
Steve Naroff26c8ea52007-03-21 21:08:52 +0000746}
747
Steve Naroff218bc2b2007-05-04 21:54:46 +0000748inline QualType Sema::CheckBitwiseOperands(
749 Expr *lex, Expr *rex, SourceLocation loc)
Steve Naroff1926c832007-04-24 00:23:05 +0000750{
751 QualType resType = UsualArithmeticConversions(lex->getType(), rex->getType());
752
Steve Naroff218bc2b2007-05-04 21:54:46 +0000753 if (resType->isIntegerType())
754 return resType;
Steve Naroffe845e272007-05-18 01:06:45 +0000755 Diag(loc, diag::err_typecheck_invalid_operands,
Chris Lattner84e160a2007-05-19 07:03:17 +0000756 lex->getType().getAsString(), rex->getType().getAsString(),
757 lex->getSourceRange(), rex->getSourceRange());
Steve Naroff218bc2b2007-05-04 21:54:46 +0000758 return QualType();
Steve Naroff26c8ea52007-03-21 21:08:52 +0000759}
760
Steve Naroff218bc2b2007-05-04 21:54:46 +0000761inline QualType Sema::CheckLogicalOperands( // C99 6.5.[13,14]
762 Expr *lex, Expr *rex, SourceLocation loc)
Steve Naroff1926c832007-04-24 00:23:05 +0000763{
Steve Naroffe4718892007-04-27 18:30:00 +0000764 QualType lhsType = UsualUnaryConversion(lex->getType());
765 QualType rhsType = UsualUnaryConversion(rex->getType());
766
Steve Naroff218bc2b2007-05-04 21:54:46 +0000767 if (lhsType->isScalarType() || rhsType->isScalarType())
768 return Context.IntTy;
Steve Naroffe845e272007-05-18 01:06:45 +0000769 Diag(loc, diag::err_typecheck_invalid_operands,
Chris Lattner84e160a2007-05-19 07:03:17 +0000770 lex->getType().getAsString(), rex->getType().getAsString(),
771 lex->getSourceRange(), rex->getSourceRange());
Steve Naroff218bc2b2007-05-04 21:54:46 +0000772 return QualType();
Steve Naroffae4143e2007-04-26 20:39:23 +0000773}
774
Steve Naroff35d85152007-05-07 00:24:15 +0000775inline QualType Sema::CheckAssignmentOperands( // C99 6.5.16.1
776 Expr *lex, Expr *rex, SourceLocation loc, QualType compoundType)
Steve Naroffae4143e2007-04-26 20:39:23 +0000777{
Steve Naroff0af91202007-04-27 21:51:21 +0000778 QualType lhsType = lex->getType();
Steve Naroff35d85152007-05-07 00:24:15 +0000779 QualType rhsType = compoundType.isNull() ? rex->getType() : compoundType;
Steve Naroff1f4d7272007-05-11 04:00:31 +0000780 bool hadError = false;
Steve Naroff218bc2b2007-05-04 21:54:46 +0000781
782 // this check is done first to give a more precise diagnostic.
Steve Naroff1f4d7272007-05-11 04:00:31 +0000783 // isModifiableLvalue() will also check for "const".
Steve Naroff218bc2b2007-05-04 21:54:46 +0000784 if (lhsType.isConstQualified()) {
Steve Naroff71ce2e02007-05-18 22:53:50 +0000785 Diag(loc, diag::err_typecheck_assign_const, lex->getSourceRange());
Steve Naroff1f4d7272007-05-11 04:00:31 +0000786 hadError = true;
787 } else if (!lex->isModifiableLvalue()) { // C99 6.5.16p2
Steve Naroff71ce2e02007-05-18 22:53:50 +0000788 Diag(loc, diag::err_typecheck_assign_non_lvalue, lex->getSourceRange());
Steve Naroff1f4d7272007-05-11 04:00:31 +0000789 return QualType(); // no need to continue checking...
Steve Naroff218bc2b2007-05-04 21:54:46 +0000790 }
791 if (lhsType == rhsType) // common case, fast path...
792 return lhsType;
793
794 AssignmentConversionResult result;
795 QualType resType = UsualAssignmentConversions(lhsType, rhsType, result);
796
797 // decode the result (notice that extensions still return a type).
798 switch (result) {
799 case Compatible:
Steve Naroff1f4d7272007-05-11 04:00:31 +0000800 break;
Steve Naroff218bc2b2007-05-04 21:54:46 +0000801 case Incompatible:
Steve Naroffe845e272007-05-18 01:06:45 +0000802 Diag(loc, diag::err_typecheck_assign_incompatible,
Chris Lattner84e160a2007-05-19 07:03:17 +0000803 lhsType.getAsString(), rhsType.getAsString(),
804 lex->getSourceRange(), rex->getSourceRange());
Steve Naroff1f4d7272007-05-11 04:00:31 +0000805 hadError = true;
806 break;
Steve Naroff218bc2b2007-05-04 21:54:46 +0000807 case PointerFromInt:
808 // check for null pointer constant (C99 6.3.2.3p3)
Steve Naroff35d85152007-05-07 00:24:15 +0000809 if (compoundType.isNull() && !rex->isNullPointerConstant())
Steve Naroff218bc2b2007-05-04 21:54:46 +0000810 Diag(loc, diag::ext_typecheck_assign_pointer_from_int);
Steve Naroff1f4d7272007-05-11 04:00:31 +0000811 break;
Steve Naroff218bc2b2007-05-04 21:54:46 +0000812 case IntFromPointer:
813 Diag(loc, diag::ext_typecheck_assign_int_from_pointer);
Steve Naroff1f4d7272007-05-11 04:00:31 +0000814 break;
Steve Naroff218bc2b2007-05-04 21:54:46 +0000815 case IncompatiblePointer:
816 Diag(loc, diag::ext_typecheck_assign_incompatible_pointer);
Steve Naroff1f4d7272007-05-11 04:00:31 +0000817 break;
818 case CompatiblePointerDiscardsQualifiers:
819 Diag(loc, diag::ext_typecheck_assign_discards_qualifiers);
820 break;
Steve Naroff218bc2b2007-05-04 21:54:46 +0000821 }
Steve Naroff1f4d7272007-05-11 04:00:31 +0000822 return hadError ? QualType() : resType;
Steve Naroffae4143e2007-04-26 20:39:23 +0000823}
824
Steve Naroff218bc2b2007-05-04 21:54:46 +0000825inline QualType Sema::CheckCommaOperands( // C99 6.5.17
Chris Lattner84e160a2007-05-19 07:03:17 +0000826 Expr *lex, Expr *rex, SourceLocation loc) {
Steve Naroff218bc2b2007-05-04 21:54:46 +0000827 return UsualUnaryConversion(rex->getType());
Steve Naroff95af0132007-03-30 23:47:58 +0000828}
829
Steve Naroff71ce2e02007-05-18 22:53:50 +0000830QualType Sema::CheckIncrementDecrementOperand(Expr *op, SourceLocation OpLoc) {
Steve Naroff094046f2007-05-13 03:21:25 +0000831 QualType resType = UsualArithmeticConversions(op->getType(), Context.IntTy);
Steve Naroff35d85152007-05-07 00:24:15 +0000832 assert(!resType.isNull() && "no type for increment/decrement expression");
Steve Naroffd50c88e2007-04-05 21:15:20 +0000833
Steve Naroff46ba1eb2007-04-03 23:13:13 +0000834 // C99 6.5.2.4p1
Steve Naroff35d85152007-05-07 00:24:15 +0000835 if (const PointerType *pt = dyn_cast<PointerType>(resType)) {
836 if (!pt->getPointeeType()->isObjectType()) { // C99 6.5.2.4p2, 6.5.6p2
Steve Naroff71ce2e02007-05-18 22:53:50 +0000837 Diag(OpLoc, diag::err_typecheck_arithmetic_incomplete_type,
838 resType.getAsString(), op->getSourceRange());
Steve Naroff35d85152007-05-07 00:24:15 +0000839 return QualType();
840 }
841 } else if (!resType->isRealType()) {
Steve Naroff95af0132007-03-30 23:47:58 +0000842 // FIXME: Allow Complex as a GCC extension.
Steve Naroff71ce2e02007-05-18 22:53:50 +0000843 Diag(OpLoc, diag::err_typecheck_illegal_increment_decrement,
844 resType.getAsString(), op->getSourceRange());
Steve Naroff35d85152007-05-07 00:24:15 +0000845 return QualType();
Steve Naroff46ba1eb2007-04-03 23:13:13 +0000846 }
Steve Naroff094046f2007-05-13 03:21:25 +0000847 // At this point, we know we have a real or pointer type. Now make sure
848 // the operand is a modifiable lvalue.
849 if (!op->isModifiableLvalue()) {
Steve Naroff71ce2e02007-05-18 22:53:50 +0000850 Diag(OpLoc, diag::err_typecheck_invalid_lvalue_incr_decr,
Chris Lattner84e160a2007-05-19 07:03:17 +0000851 op->getSourceRange());
Steve Naroff35d85152007-05-07 00:24:15 +0000852 return QualType();
853 }
854 return resType;
Steve Naroff26c8ea52007-03-21 21:08:52 +0000855}
856
Steve Naroff1926c832007-04-24 00:23:05 +0000857/// getPrimaryDeclaration - Helper function for CheckAddressOfOperand().
Steve Naroff47500512007-04-19 23:00:49 +0000858/// This routine allows us to typecheck complex/recursive expressions
859/// where the declaration is needed for type checking. Here are some
860/// examples: &s.xx, &s.zz[1].yy, &(1+2), &(XX), &"123"[2].
Steve Naroff1926c832007-04-24 00:23:05 +0000861static Decl *getPrimaryDeclaration(Expr *e) {
Steve Naroff47500512007-04-19 23:00:49 +0000862 switch (e->getStmtClass()) {
863 case Stmt::DeclRefExprClass:
864 return cast<DeclRefExpr>(e)->getDecl();
865 case Stmt::MemberExprClass:
Steve Naroff1926c832007-04-24 00:23:05 +0000866 return getPrimaryDeclaration(cast<MemberExpr>(e)->getBase());
Steve Naroff47500512007-04-19 23:00:49 +0000867 case Stmt::ArraySubscriptExprClass:
Steve Naroff1926c832007-04-24 00:23:05 +0000868 return getPrimaryDeclaration(cast<ArraySubscriptExpr>(e)->getBase());
Steve Naroff47500512007-04-19 23:00:49 +0000869 case Stmt::CallExprClass:
Steve Naroff1926c832007-04-24 00:23:05 +0000870 return getPrimaryDeclaration(cast<CallExpr>(e)->getCallee());
Steve Naroff47500512007-04-19 23:00:49 +0000871 case Stmt::UnaryOperatorClass:
Steve Naroff1926c832007-04-24 00:23:05 +0000872 return getPrimaryDeclaration(cast<UnaryOperator>(e)->getSubExpr());
Steve Naroff47500512007-04-19 23:00:49 +0000873 case Stmt::ParenExprClass:
Steve Naroff1926c832007-04-24 00:23:05 +0000874 return getPrimaryDeclaration(cast<ParenExpr>(e)->getSubExpr());
Steve Naroff47500512007-04-19 23:00:49 +0000875 default:
876 return 0;
877 }
878}
879
880/// CheckAddressOfOperand - The operand of & must be either a function
881/// designator or an lvalue designating an object. If it is an lvalue, the
882/// object cannot be declared with storage class register or be a bit field.
883/// Note: The usual conversions are *not* applied to the operand of the &
Steve Naroffa78fe7e2007-05-16 19:47:19 +0000884/// operator (C99 6.3.2.1p[2-4]), and its result is never an lvalue.
Steve Naroff35d85152007-05-07 00:24:15 +0000885QualType Sema::CheckAddressOfOperand(Expr *op, SourceLocation OpLoc) {
Steve Naroff1926c832007-04-24 00:23:05 +0000886 Decl *dcl = getPrimaryDeclaration(op);
Steve Naroff47500512007-04-19 23:00:49 +0000887
Steve Naroff5dd642e2007-05-14 18:14:51 +0000888 if (!op->isLvalue()) { // C99 6.5.3.2p1
889 if (dcl && isa<FunctionDecl>(dcl)) // allow function designators
890 ;
Steve Naroff35d85152007-05-07 00:24:15 +0000891 else {
Steve Naroff71ce2e02007-05-18 22:53:50 +0000892 Diag(OpLoc, diag::err_typecheck_invalid_lvalue_addrof,
893 op->getSourceRange());
Steve Naroff35d85152007-05-07 00:24:15 +0000894 return QualType();
895 }
Steve Naroff47500512007-04-19 23:00:49 +0000896 } else if (dcl) {
897 // We have an lvalue with a decl. Make sure the decl is not declared
898 // with the register storage-class specifier.
899 if (const VarDecl *vd = dyn_cast<VarDecl>(dcl)) {
Steve Naroff35d85152007-05-07 00:24:15 +0000900 if (vd->getStorageClass() == VarDecl::Register) {
Steve Naroff71ce2e02007-05-18 22:53:50 +0000901 Diag(OpLoc, diag::err_typecheck_address_of_register,
Chris Lattner84e160a2007-05-19 07:03:17 +0000902 op->getSourceRange());
Steve Naroff35d85152007-05-07 00:24:15 +0000903 return QualType();
904 }
Steve Narofff633d092007-04-25 19:01:39 +0000905 } else
906 assert(0 && "Unknown/unexpected decl type");
907
Steve Naroff47500512007-04-19 23:00:49 +0000908 // FIXME: add check for bitfields!
909 }
910 // If the operand has type "type", the result has type "pointer to type".
Steve Naroff35d85152007-05-07 00:24:15 +0000911 return Context.getPointerType(op->getType());
Steve Naroff47500512007-04-19 23:00:49 +0000912}
913
Steve Naroff35d85152007-05-07 00:24:15 +0000914QualType Sema::CheckIndirectionOperand(Expr *op, SourceLocation OpLoc) {
915 QualType qType = UsualUnaryConversion(op->getType());
916
Steve Naroff47500512007-04-19 23:00:49 +0000917 assert(!qType.isNull() && "no type for * expression");
918
Steve Naroff35d85152007-05-07 00:24:15 +0000919 if (PointerType *PT = dyn_cast<PointerType>(qType))
920 return PT->getPointeeType();
Steve Naroff71ce2e02007-05-18 22:53:50 +0000921 Diag(OpLoc, diag::err_typecheck_unary_expr, qType.getAsString(),
Chris Lattner84e160a2007-05-19 07:03:17 +0000922 op->getSourceRange());
Steve Naroff35d85152007-05-07 00:24:15 +0000923 return QualType();
Steve Naroff1926c832007-04-24 00:23:05 +0000924}
Steve Naroff218bc2b2007-05-04 21:54:46 +0000925
926static inline BinaryOperator::Opcode ConvertTokenKindToBinaryOpcode(
927 tok::TokenKind Kind) {
928 BinaryOperator::Opcode Opc;
929 switch (Kind) {
930 default: assert(0 && "Unknown binop!");
931 case tok::star: Opc = BinaryOperator::Mul; break;
932 case tok::slash: Opc = BinaryOperator::Div; break;
933 case tok::percent: Opc = BinaryOperator::Rem; break;
934 case tok::plus: Opc = BinaryOperator::Add; break;
935 case tok::minus: Opc = BinaryOperator::Sub; break;
936 case tok::lessless: Opc = BinaryOperator::Shl; break;
937 case tok::greatergreater: Opc = BinaryOperator::Shr; break;
938 case tok::lessequal: Opc = BinaryOperator::LE; break;
939 case tok::less: Opc = BinaryOperator::LT; break;
940 case tok::greaterequal: Opc = BinaryOperator::GE; break;
941 case tok::greater: Opc = BinaryOperator::GT; break;
942 case tok::exclaimequal: Opc = BinaryOperator::NE; break;
943 case tok::equalequal: Opc = BinaryOperator::EQ; break;
944 case tok::amp: Opc = BinaryOperator::And; break;
945 case tok::caret: Opc = BinaryOperator::Xor; break;
946 case tok::pipe: Opc = BinaryOperator::Or; break;
947 case tok::ampamp: Opc = BinaryOperator::LAnd; break;
948 case tok::pipepipe: Opc = BinaryOperator::LOr; break;
949 case tok::equal: Opc = BinaryOperator::Assign; break;
950 case tok::starequal: Opc = BinaryOperator::MulAssign; break;
951 case tok::slashequal: Opc = BinaryOperator::DivAssign; break;
952 case tok::percentequal: Opc = BinaryOperator::RemAssign; break;
953 case tok::plusequal: Opc = BinaryOperator::AddAssign; break;
954 case tok::minusequal: Opc = BinaryOperator::SubAssign; break;
955 case tok::lesslessequal: Opc = BinaryOperator::ShlAssign; break;
956 case tok::greatergreaterequal: Opc = BinaryOperator::ShrAssign; break;
957 case tok::ampequal: Opc = BinaryOperator::AndAssign; break;
958 case tok::caretequal: Opc = BinaryOperator::XorAssign; break;
959 case tok::pipeequal: Opc = BinaryOperator::OrAssign; break;
960 case tok::comma: Opc = BinaryOperator::Comma; break;
961 }
962 return Opc;
963}
964
Steve Naroff35d85152007-05-07 00:24:15 +0000965static inline UnaryOperator::Opcode ConvertTokenKindToUnaryOpcode(
966 tok::TokenKind Kind) {
967 UnaryOperator::Opcode Opc;
968 switch (Kind) {
969 default: assert(0 && "Unknown unary op!");
970 case tok::plusplus: Opc = UnaryOperator::PreInc; break;
971 case tok::minusminus: Opc = UnaryOperator::PreDec; break;
972 case tok::amp: Opc = UnaryOperator::AddrOf; break;
973 case tok::star: Opc = UnaryOperator::Deref; break;
974 case tok::plus: Opc = UnaryOperator::Plus; break;
975 case tok::minus: Opc = UnaryOperator::Minus; break;
976 case tok::tilde: Opc = UnaryOperator::Not; break;
977 case tok::exclaim: Opc = UnaryOperator::LNot; break;
978 case tok::kw_sizeof: Opc = UnaryOperator::SizeOf; break;
979 case tok::kw___alignof: Opc = UnaryOperator::AlignOf; break;
980 case tok::kw___real: Opc = UnaryOperator::Real; break;
981 case tok::kw___imag: Opc = UnaryOperator::Imag; break;
982 case tok::ampamp: Opc = UnaryOperator::AddrLabel; break;
983 // FIXME: case tok::kw___extension__:
984 }
985 return Opc;
986}
987
Steve Naroff218bc2b2007-05-04 21:54:46 +0000988// Binary Operators. 'Tok' is the token for the operator.
989Action::ExprResult Sema::ParseBinOp(SourceLocation TokLoc, tok::TokenKind Kind,
990 ExprTy *LHS, ExprTy *RHS) {
991 BinaryOperator::Opcode Opc = ConvertTokenKindToBinaryOpcode(Kind);
992 Expr *lhs = (Expr *)LHS, *rhs = (Expr*)RHS;
993
994 assert((lhs != 0) && "ParseBinOp(): missing left expression");
995 assert((rhs != 0) && "ParseBinOp(): missing right expression");
996
997 QualType result;
998
999 switch (Opc) {
1000 default:
1001 assert(0 && "Unknown binary expr!");
1002 case BinaryOperator::Assign:
Steve Naroff35d85152007-05-07 00:24:15 +00001003 result = CheckAssignmentOperands(lhs, rhs, TokLoc, QualType());
Steve Naroff218bc2b2007-05-04 21:54:46 +00001004 break;
1005 case BinaryOperator::Mul:
1006 case BinaryOperator::Div:
1007 result = CheckMultiplyDivideOperands(lhs, rhs, TokLoc);
1008 break;
1009 case BinaryOperator::Rem:
1010 result = CheckRemainderOperands(lhs, rhs, TokLoc);
1011 break;
1012 case BinaryOperator::Add:
1013 result = CheckAdditionOperands(lhs, rhs, TokLoc);
1014 break;
1015 case BinaryOperator::Sub:
1016 result = CheckSubtractionOperands(lhs, rhs, TokLoc);
1017 break;
1018 case BinaryOperator::Shl:
1019 case BinaryOperator::Shr:
1020 result = CheckShiftOperands(lhs, rhs, TokLoc);
1021 break;
1022 case BinaryOperator::LE:
1023 case BinaryOperator::LT:
1024 case BinaryOperator::GE:
1025 case BinaryOperator::GT:
1026 result = CheckRelationalOperands(lhs, rhs, TokLoc);
1027 break;
1028 case BinaryOperator::EQ:
1029 case BinaryOperator::NE:
1030 result = CheckEqualityOperands(lhs, rhs, TokLoc);
1031 break;
1032 case BinaryOperator::And:
1033 case BinaryOperator::Xor:
1034 case BinaryOperator::Or:
1035 result = CheckBitwiseOperands(lhs, rhs, TokLoc);
1036 break;
1037 case BinaryOperator::LAnd:
1038 case BinaryOperator::LOr:
1039 result = CheckLogicalOperands(lhs, rhs, TokLoc);
1040 break;
1041 case BinaryOperator::MulAssign:
1042 case BinaryOperator::DivAssign:
1043 result = CheckMultiplyDivideOperands(lhs, rhs, TokLoc);
1044 if (result.isNull())
1045 return true;
Steve Naroff35d85152007-05-07 00:24:15 +00001046 result = CheckAssignmentOperands(lhs, rhs, TokLoc, result);
Steve Naroff218bc2b2007-05-04 21:54:46 +00001047 break;
1048 case BinaryOperator::RemAssign:
1049 result = CheckRemainderOperands(lhs, rhs, TokLoc);
1050 if (result.isNull())
1051 return true;
Steve Naroff35d85152007-05-07 00:24:15 +00001052 result = CheckAssignmentOperands(lhs, rhs, TokLoc, result);
Steve Naroff218bc2b2007-05-04 21:54:46 +00001053 break;
1054 case BinaryOperator::AddAssign:
1055 result = CheckAdditionOperands(lhs, rhs, TokLoc);
1056 if (result.isNull())
1057 return true;
Steve Naroff35d85152007-05-07 00:24:15 +00001058 result = CheckAssignmentOperands(lhs, rhs, TokLoc, result);
Steve Naroff218bc2b2007-05-04 21:54:46 +00001059 break;
1060 case BinaryOperator::SubAssign:
1061 result = CheckSubtractionOperands(lhs, rhs, TokLoc);
1062 if (result.isNull())
1063 return true;
Steve Naroff35d85152007-05-07 00:24:15 +00001064 result = CheckAssignmentOperands(lhs, rhs, TokLoc, result);
Steve Naroff218bc2b2007-05-04 21:54:46 +00001065 break;
1066 case BinaryOperator::ShlAssign:
1067 case BinaryOperator::ShrAssign:
1068 result = CheckShiftOperands(lhs, rhs, TokLoc);
1069 if (result.isNull())
1070 return true;
Steve Naroff35d85152007-05-07 00:24:15 +00001071 result = CheckAssignmentOperands(lhs, rhs, TokLoc, result);
Steve Naroff218bc2b2007-05-04 21:54:46 +00001072 break;
1073 case BinaryOperator::AndAssign:
1074 case BinaryOperator::XorAssign:
1075 case BinaryOperator::OrAssign:
1076 result = CheckBitwiseOperands(lhs, rhs, TokLoc);
1077 if (result.isNull())
1078 return true;
Steve Naroff35d85152007-05-07 00:24:15 +00001079 result = CheckAssignmentOperands(lhs, rhs, TokLoc, result);
Steve Naroff218bc2b2007-05-04 21:54:46 +00001080 break;
1081 case BinaryOperator::Comma:
1082 result = CheckCommaOperands(lhs, rhs, TokLoc);
1083 break;
1084 }
1085 if (result.isNull())
1086 return true;
1087 return new BinaryOperator(lhs, rhs, Opc, result);
1088}
1089
Steve Naroff35d85152007-05-07 00:24:15 +00001090// Unary Operators. 'Tok' is the token for the operator.
1091Action::ExprResult Sema::ParseUnaryOp(SourceLocation OpLoc, tok::TokenKind Op,
1092 ExprTy *Input) {
1093 UnaryOperator::Opcode Opc = ConvertTokenKindToUnaryOpcode(Op);
1094 QualType resultType;
1095 switch (Opc) {
1096 default:
1097 assert(0 && "Unimplemented unary expr!");
1098 case UnaryOperator::PreInc:
1099 case UnaryOperator::PreDec:
Steve Naroff71ce2e02007-05-18 22:53:50 +00001100 resultType = CheckIncrementDecrementOperand((Expr *)Input, OpLoc);
Steve Naroff35d85152007-05-07 00:24:15 +00001101 break;
1102 case UnaryOperator::AddrOf:
1103 resultType = CheckAddressOfOperand((Expr *)Input, OpLoc);
1104 break;
1105 case UnaryOperator::Deref:
1106 resultType = CheckIndirectionOperand((Expr *)Input, OpLoc);
1107 break;
1108 case UnaryOperator::Plus:
1109 case UnaryOperator::Minus:
1110 resultType = UsualUnaryConversion(((Expr *)Input)->getType());
1111 if (!resultType->isArithmeticType()) // C99 6.5.3.3p1
Chris Lattnerc04bd6a2007-05-16 18:09:54 +00001112 return Diag(OpLoc, diag::err_typecheck_unary_expr,
1113 resultType.getAsString());
Steve Naroff35d85152007-05-07 00:24:15 +00001114 break;
1115 case UnaryOperator::Not: // bitwise complement
1116 resultType = UsualUnaryConversion(((Expr *)Input)->getType());
1117 if (!resultType->isIntegerType()) // C99 6.5.3.3p1
Chris Lattnerc04bd6a2007-05-16 18:09:54 +00001118 return Diag(OpLoc, diag::err_typecheck_unary_expr,
1119 resultType.getAsString());
Steve Naroff35d85152007-05-07 00:24:15 +00001120 break;
1121 case UnaryOperator::LNot: // logical negation
1122 resultType = UsualUnaryConversion(((Expr *)Input)->getType());
1123 if (!resultType->isScalarType()) // C99 6.5.3.3p1
Chris Lattnerc04bd6a2007-05-16 18:09:54 +00001124 return Diag(OpLoc, diag::err_typecheck_unary_expr,
1125 resultType.getAsString());
Steve Naroff35d85152007-05-07 00:24:15 +00001126 break;
1127 case UnaryOperator::SizeOf:
Steve Naroff043d45d2007-05-15 02:32:35 +00001128 resultType = CheckSizeOfAlignOfOperand(((Expr *)Input)->getType(), OpLoc,
1129 true);
1130 break;
Steve Naroff35d85152007-05-07 00:24:15 +00001131 case UnaryOperator::AlignOf:
Steve Naroff043d45d2007-05-15 02:32:35 +00001132 resultType = CheckSizeOfAlignOfOperand(((Expr *)Input)->getType(), OpLoc,
1133 false);
1134 break;
Steve Naroff35d85152007-05-07 00:24:15 +00001135 }
1136 if (resultType.isNull())
1137 return true;
Steve Naroff509fe022007-05-17 01:16:00 +00001138 return new UnaryOperator((Expr *)Input, Opc, resultType, OpLoc);
Steve Naroff35d85152007-05-07 00:24:15 +00001139}