blob: 2e84eb35bf9eef88601ee65cbea8f1a58afec607 [file] [log] [blame]
Chris Lattner5b183d82006-11-10 05:03:26 +00001//===--- SemaExpr.cpp - Semantic Analysis for Expressions -----------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file was developed by Chris Lattner and is distributed under
6// the University of Illinois Open Source License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file implements semantic analysis for expressions.
11//
12//===----------------------------------------------------------------------===//
13
14#include "Sema.h"
Chris Lattnercb6a3822006-11-10 06:20:45 +000015#include "clang/AST/ASTContext.h"
Chris Lattner17ed4872006-11-20 04:58:19 +000016#include "clang/AST/Decl.h"
Chris Lattner5b183d82006-11-10 05:03:26 +000017#include "clang/AST/Expr.h"
18#include "clang/Lex/Preprocessor.h"
Steve Naroff09ef4742007-03-09 23:16:33 +000019#include "clang/Lex/LiteralSupport.h"
Steve Narofff2fb89e2007-03-13 20:29:44 +000020#include "clang/Basic/SourceManager.h"
Chris Lattner5b183d82006-11-10 05:03:26 +000021#include "clang/Basic/Diagnostic.h"
Chris Lattnerac18be92006-11-20 06:49:47 +000022#include "clang/Basic/LangOptions.h"
Chris Lattner5b183d82006-11-10 05:03:26 +000023#include "clang/Basic/TargetInfo.h"
24#include "llvm/ADT/SmallString.h"
Chris Lattner5b183d82006-11-10 05:03:26 +000025using namespace llvm;
26using namespace clang;
27
Steve Naroffdf7855b2007-02-21 23:46:25 +000028/// ParseStringLiteral - The specified tokens were lexed as pasted string
Chris Lattner5b183d82006-11-10 05:03:26 +000029/// fragments (e.g. "foo" "bar" L"baz"). The result string has to handle string
30/// concatenation ([C99 5.1.1.2, translation phase #6]), so it may come from
31/// multiple tokens. However, the common case is that StringToks points to one
32/// string.
33///
34Action::ExprResult
Steve Naroffdf7855b2007-02-21 23:46:25 +000035Sema::ParseStringLiteral(const LexerToken *StringToks, unsigned NumStringToks) {
Chris Lattner5b183d82006-11-10 05:03:26 +000036 assert(NumStringToks && "Must have at least one string!");
37
Steve Naroff4f88b312007-03-13 22:37:02 +000038 StringLiteralParser Literal(StringToks, NumStringToks, PP, Context.Target);
39 if (Literal.hadError)
40 return ExprResult(true);
Chris Lattner5b183d82006-11-10 05:03:26 +000041
Chris Lattner5b183d82006-11-10 05:03:26 +000042 SmallVector<SourceLocation, 4> StringTokLocs;
43 for (unsigned i = 0; i != NumStringToks; ++i)
44 StringTokLocs.push_back(StringToks[i].getLocation());
Steve Narofff1e53692007-03-23 22:27:02 +000045
46 // FIXME: handle wchar_t
Steve Naroffe5aa9be2007-04-05 22:36:20 +000047 QualType t = Context.getPointerType(Context.CharTy);
Steve Narofff1e53692007-03-23 22:27:02 +000048
Chris Lattner5b183d82006-11-10 05:03:26 +000049 // FIXME: use factory.
Chris Lattner5b183d82006-11-10 05:03:26 +000050 // Pass &StringTokLocs[0], StringTokLocs.size() to factory!
Steve Naroff4f88b312007-03-13 22:37:02 +000051 return new StringLiteral(Literal.GetString(), Literal.GetStringLength(),
Steve Naroff53f07dc2007-05-17 21:49:33 +000052 Literal.AnyWide, t, StringToks[0].getLocation(),
53 StringToks[NumStringToks-1].getLocation());
Chris Lattner5b183d82006-11-10 05:03:26 +000054}
55
Chris Lattnere168f762006-11-10 05:29:30 +000056
Chris Lattnerac18be92006-11-20 06:49:47 +000057/// ParseIdentifierExpr - The parser read an identifier in expression context,
58/// validate it per-C99 6.5.1. HasTrailingLParen indicates whether this
59/// identifier is used in an function call context.
60Sema::ExprResult Sema::ParseIdentifierExpr(Scope *S, SourceLocation Loc,
61 IdentifierInfo &II,
62 bool HasTrailingLParen) {
Chris Lattner17ed4872006-11-20 04:58:19 +000063 // Could be enum-constant or decl.
Chris Lattner9561a0b2007-01-28 08:20:04 +000064 Decl *D = LookupScopedDecl(&II, Decl::IDNS_Ordinary, Loc, S);
Chris Lattner17ed4872006-11-20 04:58:19 +000065 if (D == 0) {
Bill Wendling4073ed52007-02-13 01:51:42 +000066 // Otherwise, this could be an implicitly declared function reference (legal
Chris Lattner9561a0b2007-01-28 08:20:04 +000067 // in C90, extension in C99).
Chris Lattnerac18be92006-11-20 06:49:47 +000068 if (HasTrailingLParen &&
69 // Not in C++.
Steve Narofff1e53692007-03-23 22:27:02 +000070 !getLangOptions().CPlusPlus)
Chris Lattnerac18be92006-11-20 06:49:47 +000071 D = ImplicitlyDefineFunction(Loc, II, S);
Steve Naroff92e30f82007-04-02 22:35:25 +000072 else {
Chris Lattnerac18be92006-11-20 06:49:47 +000073 // If this name wasn't predeclared and if this is not a function call,
74 // diagnose the problem.
Steve Narofff1e53692007-03-23 22:27:02 +000075 return Diag(Loc, diag::err_undeclared_var_use, II.getName());
Steve Naroff92e30f82007-04-02 22:35:25 +000076 }
Chris Lattner17ed4872006-11-20 04:58:19 +000077 }
78
Steve Naroff46ba1eb2007-04-03 23:13:13 +000079 if (ValueDecl *VD = dyn_cast<ValueDecl>(D))
Steve Naroff509fe022007-05-17 01:16:00 +000080 return new DeclRefExpr(VD, VD->getType(), Loc);
Steve Naroff46ba1eb2007-04-03 23:13:13 +000081 if (isa<TypedefDecl>(D))
Steve Narofff1e53692007-03-23 22:27:02 +000082 return Diag(Loc, diag::err_unexpected_typedef, II.getName());
83
84 assert(0 && "Invalid decl");
Chris Lattner17ed4872006-11-20 04:58:19 +000085}
Chris Lattnere168f762006-11-10 05:29:30 +000086
Chris Lattner17ed4872006-11-20 04:58:19 +000087Sema::ExprResult Sema::ParseSimplePrimaryExpr(SourceLocation Loc,
88 tok::TokenKind Kind) {
Chris Lattnere168f762006-11-10 05:29:30 +000089 switch (Kind) {
90 default:
91 assert(0 && "Unknown simple primary expr!");
Steve Naroffe4718892007-04-27 18:30:00 +000092 // TODO: MOVE this to be some other callback.
Chris Lattnere168f762006-11-10 05:29:30 +000093 case tok::kw___func__: // primary-expression: __func__ [C99 6.4.2.2]
94 case tok::kw___FUNCTION__: // primary-expression: __FUNCTION__ [GNU]
95 case tok::kw___PRETTY_FUNCTION__: // primary-expression: __P..Y_F..N__ [GNU]
Chris Lattner17ed4872006-11-20 04:58:19 +000096 return 0;
Chris Lattnere168f762006-11-10 05:29:30 +000097 }
98}
99
Steve Naroffae4143e2007-04-26 20:39:23 +0000100Sema::ExprResult Sema::ParseCharacterConstant(const LexerToken &Tok) {
101 SmallString<16> CharBuffer;
102 CharBuffer.resize(Tok.getLength());
103 const char *ThisTokBegin = &CharBuffer[0];
104 unsigned ActualLength = PP.getSpelling(Tok, ThisTokBegin);
105
106 CharLiteralParser Literal(ThisTokBegin, ThisTokBegin+ActualLength,
107 Tok.getLocation(), PP);
108 if (Literal.hadError())
109 return ExprResult(true);
Steve Naroff509fe022007-05-17 01:16:00 +0000110 return new CharacterLiteral(Literal.getValue(), Context.IntTy,
111 Tok.getLocation());
Steve Naroffae4143e2007-04-26 20:39:23 +0000112}
113
Steve Naroff8160ea22007-03-06 01:09:46 +0000114Action::ExprResult Sema::ParseNumericConstant(const LexerToken &Tok) {
Steve Narofff2fb89e2007-03-13 20:29:44 +0000115 // fast path for a single digit (which is quite common). A single digit
116 // cannot have a trigraph, escaped newline, radix prefix, or type suffix.
117 if (Tok.getLength() == 1) {
118 const char *t = PP.getSourceManager().getCharacterData(Tok.getLocation());
Chris Lattner67ca9252007-05-21 01:08:44 +0000119
120 unsigned IntSize = Context.Target.getIntWidth(Tok.getLocation());
121 return ExprResult(new IntegerLiteral(APInt(IntSize, *t-'0'), Context.IntTy,
Steve Naroff509fe022007-05-17 01:16:00 +0000122 Tok.getLocation()));
Steve Narofff2fb89e2007-03-13 20:29:44 +0000123 }
Steve Naroff8160ea22007-03-06 01:09:46 +0000124 SmallString<512> IntegerBuffer;
125 IntegerBuffer.resize(Tok.getLength());
126 const char *ThisTokBegin = &IntegerBuffer[0];
127
Chris Lattner67ca9252007-05-21 01:08:44 +0000128 // Get the spelling of the token, which eliminates trigraphs, etc.
Steve Naroff8160ea22007-03-06 01:09:46 +0000129 unsigned ActualLength = PP.getSpelling(Tok, ThisTokBegin);
Steve Naroff09ef4742007-03-09 23:16:33 +0000130 NumericLiteralParser Literal(ThisTokBegin, ThisTokBegin+ActualLength,
Steve Naroff451d8f162007-03-12 23:22:38 +0000131 Tok.getLocation(), PP);
Steve Narofff2fb89e2007-03-13 20:29:44 +0000132 if (Literal.hadError)
133 return ExprResult(true);
Chris Lattner67ca9252007-05-21 01:08:44 +0000134
Steve Naroff09ef4742007-03-09 23:16:33 +0000135 if (Literal.isIntegerLiteral()) {
Steve Naroffe5aa9be2007-04-05 22:36:20 +0000136 QualType t;
Chris Lattner67ca9252007-05-21 01:08:44 +0000137
138 // Get the value in the widest-possible width.
139 APInt ResultVal(Context.Target.getIntMaxTWidth(Tok.getLocation()), 0);
140
141 if (Literal.GetIntegerValue(ResultVal)) {
142 // If this value didn't fit into uintmax_t, warn and force to ull.
143 Diag(Tok.getLocation(), diag::warn_integer_too_large);
144 t = Context.UnsignedLongLongTy;
145 assert(Context.getIntegerBitwidth(t, Tok.getLocation()) ==
146 ResultVal.getBitWidth() && "long long is not intmax_t?");
Steve Naroff09ef4742007-03-09 23:16:33 +0000147 } else {
Chris Lattner67ca9252007-05-21 01:08:44 +0000148 // If this value fits into a ULL, try to figure out what else it fits into
149 // according to the rules of C99 6.4.4.1p5.
150
151 // Octal, Hexadecimal, and integers with a U suffix are allowed to
152 // be an unsigned int.
153 bool AllowUnsigned = Literal.isUnsigned || Literal.getRadix() != 10;
154
155 // Check from smallest to largest, picking the smallest type we can.
156 if (!Literal.isLong) { // Are int/unsigned possibilities?
157 unsigned IntSize = Context.Target.getIntWidth(Tok.getLocation());
158 // Does it fit in a unsigned int?
159 if (ResultVal.isIntN(IntSize)) {
160 // Does it fit in a signed int?
161 if (!Literal.isUnsigned && ResultVal[IntSize-1] == 0)
162 t = Context.IntTy;
163 else if (AllowUnsigned)
164 t = Context.UnsignedIntTy;
165 }
166
167 if (!t.isNull())
168 ResultVal.trunc(IntSize);
169 }
170
171 // Are long/unsigned long possibilities?
172 if (t.isNull() && !Literal.isLongLong) {
173 unsigned LongSize = Context.Target.getLongWidth(Tok.getLocation());
174
175 // Does it fit in a unsigned long?
176 if (ResultVal.isIntN(LongSize)) {
177 // Does it fit in a signed long?
178 if (!Literal.isUnsigned && ResultVal[LongSize-1] == 0)
179 t = Context.LongTy;
180 else if (AllowUnsigned)
181 t = Context.UnsignedLongTy;
182 }
183 if (!t.isNull())
184 ResultVal.trunc(LongSize);
185 }
186
187 // Finally, check long long if needed.
188 if (t.isNull()) {
189 unsigned LongLongSize =
190 Context.Target.getLongLongWidth(Tok.getLocation());
191
192 // Does it fit in a unsigned long long?
193 if (ResultVal.isIntN(LongLongSize)) {
194 // Does it fit in a signed long long?
195 if (!Literal.isUnsigned && ResultVal[LongLongSize-1] == 0)
196 t = Context.LongLongTy;
197 else if (AllowUnsigned)
198 t = Context.UnsignedLongLongTy;
199 }
200 }
201
202 // If we still couldn't decide a type, we probably have something that
203 // does not fit in a signed long long, but has no U suffix.
204 if (t.isNull()) {
205 Diag(Tok.getLocation(), diag::warn_integer_too_large_for_signed);
206 t = Context.UnsignedLongLongTy;
207 }
Steve Naroff09ef4742007-03-09 23:16:33 +0000208 }
Chris Lattner67ca9252007-05-21 01:08:44 +0000209
210 return new IntegerLiteral(ResultVal, t, Tok.getLocation());
Steve Naroff09ef4742007-03-09 23:16:33 +0000211 } else if (Literal.isFloatingLiteral()) {
Steve Narofff1e53692007-03-23 22:27:02 +0000212 // FIXME: fill in the value and compute the real type...
Steve Naroff509fe022007-05-17 01:16:00 +0000213 return new FloatingLiteral(7.7, Context.FloatTy, Tok.getLocation());
Steve Naroff09ef4742007-03-09 23:16:33 +0000214 }
Steve Narofff2fb89e2007-03-13 20:29:44 +0000215 return ExprResult(true);
Chris Lattnere168f762006-11-10 05:29:30 +0000216}
217
218Action::ExprResult Sema::ParseParenExpr(SourceLocation L, SourceLocation R,
219 ExprTy *Val) {
Steve Naroffae4143e2007-04-26 20:39:23 +0000220 Expr *e = (Expr *)Val;
221 assert((e != 0) && "ParseParenExpr() missing expr");
Steve Naroff53f07dc2007-05-17 21:49:33 +0000222 return new ParenExpr(L, R, e);
Chris Lattnere168f762006-11-10 05:29:30 +0000223}
224
Steve Naroffa78fe7e2007-05-16 19:47:19 +0000225/// The UsualUnaryConversion() function is *not* called by this routine.
226/// See C99 6.3.2.1p[2-4] for more details.
Steve Naroff043d45d2007-05-15 02:32:35 +0000227QualType Sema::CheckSizeOfAlignOfOperand(QualType exprType,
228 SourceLocation OpLoc, bool isSizeof) {
229 // C99 6.5.3.4p1:
230 if (isa<FunctionType>(exprType) && isSizeof)
231 // alignof(function) is allowed.
232 Diag(OpLoc, diag::ext_sizeof_function_type);
233 else if (exprType->isVoidType())
234 Diag(OpLoc, diag::ext_sizeof_void_type, isSizeof ? "sizeof" : "__alignof");
235 else if (exprType->isIncompleteType()) {
Steve Naroff043d45d2007-05-15 02:32:35 +0000236 Diag(OpLoc, isSizeof ? diag::err_sizeof_incomplete_type :
Chris Lattner3dc3d772007-05-16 18:07:12 +0000237 diag::err_alignof_incomplete_type,
238 exprType.getAsString());
Steve Naroff043d45d2007-05-15 02:32:35 +0000239 return QualType(); // error
240 }
241 // C99 6.5.3.4p4: the type (an unsigned integer type) is size_t.
242 return Context.getSizeType();
243}
244
Chris Lattnere168f762006-11-10 05:29:30 +0000245Action::ExprResult Sema::
246ParseSizeOfAlignOfTypeExpr(SourceLocation OpLoc, bool isSizeof,
Steve Naroff509fe022007-05-17 01:16:00 +0000247 SourceLocation LPLoc, TypeTy *Ty,
248 SourceLocation RPLoc) {
Chris Lattner0d8b1a12006-11-20 04:34:45 +0000249 // If error parsing type, ignore.
250 if (Ty == 0) return true;
Chris Lattner6531c102007-01-23 22:29:49 +0000251
252 // Verify that this is a valid expression.
Steve Naroffe5aa9be2007-04-05 22:36:20 +0000253 QualType ArgTy = QualType::getFromOpaquePtr(Ty);
Chris Lattner6531c102007-01-23 22:29:49 +0000254
Steve Naroff043d45d2007-05-15 02:32:35 +0000255 QualType resultType = CheckSizeOfAlignOfOperand(ArgTy, OpLoc, isSizeof);
256
257 if (resultType.isNull())
258 return true;
Steve Naroff509fe022007-05-17 01:16:00 +0000259 return new SizeOfAlignOfTypeExpr(isSizeof, ArgTy, resultType, OpLoc, RPLoc);
Chris Lattnere168f762006-11-10 05:29:30 +0000260}
261
262
263Action::ExprResult Sema::ParsePostfixUnaryOp(SourceLocation OpLoc,
264 tok::TokenKind Kind,
265 ExprTy *Input) {
266 UnaryOperator::Opcode Opc;
267 switch (Kind) {
268 default: assert(0 && "Unknown unary op!");
269 case tok::plusplus: Opc = UnaryOperator::PostInc; break;
270 case tok::minusminus: Opc = UnaryOperator::PostDec; break;
271 }
Steve Naroff71ce2e02007-05-18 22:53:50 +0000272 QualType result = CheckIncrementDecrementOperand((Expr *)Input, OpLoc);
Steve Naroff35d85152007-05-07 00:24:15 +0000273 if (result.isNull())
274 return true;
Steve Naroff509fe022007-05-17 01:16:00 +0000275 return new UnaryOperator((Expr *)Input, Opc, result, OpLoc);
Chris Lattnere168f762006-11-10 05:29:30 +0000276}
277
278Action::ExprResult Sema::
279ParseArraySubscriptExpr(ExprTy *Base, SourceLocation LLoc,
280 ExprTy *Idx, SourceLocation RLoc) {
Steve Naroffe5aa9be2007-04-05 22:36:20 +0000281 QualType t1 = ((Expr *)Base)->getType();
282 QualType t2 = ((Expr *)Idx)->getType();
Steve Narofff1e53692007-03-23 22:27:02 +0000283
284 assert(!t1.isNull() && "no type for array base expression");
Steve Naroffc1aadb12007-03-28 21:49:40 +0000285 assert(!t2.isNull() && "no type for array index expression");
Steve Narofff1e53692007-03-23 22:27:02 +0000286
Steve Naroffe5aa9be2007-04-05 22:36:20 +0000287 QualType canonT1 = t1.getCanonicalType();
288 QualType canonT2 = t2.getCanonicalType();
Steve Naroffd50c88e2007-04-05 21:15:20 +0000289
Steve Naroffc1aadb12007-03-28 21:49:40 +0000290 // C99 6.5.2.1p2: the expression e1[e2] is by definition precisely equivalent
291 // to the expression *((e1)+(e2)). This means the array "Base" may actually be
Steve Narofff1e53692007-03-23 22:27:02 +0000292 // in the subscript position. As a result, we need to derive the array base
293 // and index from the expression types.
294
Steve Naroffe5aa9be2007-04-05 22:36:20 +0000295 QualType baseType, indexType;
Steve Naroffd50c88e2007-04-05 21:15:20 +0000296 if (isa<ArrayType>(canonT1) || isa<PointerType>(canonT1)) {
297 baseType = canonT1;
298 indexType = canonT2;
299 } else if (isa<ArrayType>(canonT2) || isa<PointerType>(canonT2)) { // uncommon
300 baseType = canonT2;
301 indexType = canonT1;
Steve Narofff1e53692007-03-23 22:27:02 +0000302 } else
303 return Diag(LLoc, diag::err_typecheck_subscript_value);
304
Steve Naroffc1aadb12007-03-28 21:49:40 +0000305 // C99 6.5.2.1p1
Steve Naroff1926c832007-04-24 00:23:05 +0000306 if (!indexType->isIntegerType())
Steve Narofff1e53692007-03-23 22:27:02 +0000307 return Diag(LLoc, diag::err_typecheck_subscript);
Steve Naroffc1aadb12007-03-28 21:49:40 +0000308
Steve Naroff95af0132007-03-30 23:47:58 +0000309 // FIXME: need to deal with const...
Steve Naroffe5aa9be2007-04-05 22:36:20 +0000310 QualType resultType;
Steve Naroffc1aadb12007-03-28 21:49:40 +0000311 if (ArrayType *ary = dyn_cast<ArrayType>(baseType)) {
312 resultType = ary->getElementType();
313 } else if (PointerType *ary = dyn_cast<PointerType>(baseType)) {
314 resultType = ary->getPointeeType();
315 // in practice, the following check catches trying to index a pointer
316 // to a function (e.g. void (*)(int)). Functions are not objects in c99.
Steve Naroffbc2f0992007-03-30 20:09:34 +0000317 if (!resultType->isObjectType())
Chris Lattnerc04bd6a2007-05-16 18:09:54 +0000318 return Diag(LLoc, diag::err_typecheck_subscript_not_object,
319 baseType.getAsString());
Steve Naroffc1aadb12007-03-28 21:49:40 +0000320 }
Steve Naroff509fe022007-05-17 01:16:00 +0000321 return new ArraySubscriptExpr((Expr*)Base, (Expr*)Idx, resultType, RLoc);
Chris Lattnere168f762006-11-10 05:29:30 +0000322}
323
324Action::ExprResult Sema::
325ParseMemberReferenceExpr(ExprTy *Base, SourceLocation OpLoc,
326 tok::TokenKind OpKind, SourceLocation MemberLoc,
327 IdentifierInfo &Member) {
Steve Naroffe5aa9be2007-04-05 22:36:20 +0000328 QualType qualifiedType = ((Expr *)Base)->getType();
Steve Naroffca8f7122007-04-01 01:41:35 +0000329
330 assert(!qualifiedType.isNull() && "no type for member expression");
331
Steve Naroffe5aa9be2007-04-05 22:36:20 +0000332 QualType canonType = qualifiedType.getCanonicalType();
Steve Narofff1e53692007-03-23 22:27:02 +0000333
334 if (OpKind == tok::arrow) {
Steve Naroffca8f7122007-04-01 01:41:35 +0000335 if (PointerType *PT = dyn_cast<PointerType>(canonType)) {
336 qualifiedType = PT->getPointeeType();
Steve Naroffd50c88e2007-04-05 21:15:20 +0000337 canonType = qualifiedType.getCanonicalType();
Steve Naroffca8f7122007-04-01 01:41:35 +0000338 } else
Steve Narofff1e53692007-03-23 22:27:02 +0000339 return Diag(OpLoc, diag::err_typecheck_member_reference_arrow);
340 }
Steve Naroff92e30f82007-04-02 22:35:25 +0000341 if (!isa<RecordType>(canonType))
342 return Diag(OpLoc, diag::err_typecheck_member_reference_structUnion);
343
344 // get the struct/union definition from the type.
345 RecordDecl *RD = cast<RecordType>(canonType)->getDecl();
Steve Naroffcc321422007-03-26 23:09:51 +0000346
Steve Naroff92e30f82007-04-02 22:35:25 +0000347 if (canonType->isIncompleteType())
348 return Diag(OpLoc, diag::err_typecheck_incomplete_tag, RD->getName());
Steve Naroffcc321422007-03-26 23:09:51 +0000349
Steve Naroff92e30f82007-04-02 22:35:25 +0000350 FieldDecl *MemberDecl = RD->getMember(&Member);
351 if (!MemberDecl)
352 return Diag(OpLoc, diag::err_typecheck_no_member, Member.getName());
353
Steve Naroff9358c712007-05-27 23:58:33 +0000354 return new MemberExpr((Expr*)Base, OpKind == tok::arrow,
355 MemberDecl, MemberLoc);
Chris Lattnere168f762006-11-10 05:29:30 +0000356}
357
358/// ParseCallExpr - Handle a call to Fn with the specified array of arguments.
359/// This provides the location of the left/right parens and a list of comma
360/// locations.
361Action::ExprResult Sema::
362ParseCallExpr(ExprTy *Fn, SourceLocation LParenLoc,
Steve Naroffb8c289d2007-05-08 22:18:00 +0000363 ExprTy **Args, unsigned NumArgsInCall,
Chris Lattnere168f762006-11-10 05:29:30 +0000364 SourceLocation *CommaLocs, SourceLocation RParenLoc) {
Steve Naroff8563f652007-05-28 19:25:56 +0000365 Expr *funcExpr = (Expr *)Fn;
Steve Naroff8563f652007-05-28 19:25:56 +0000366 assert(funcExpr && "no function call expression");
367
368 QualType qType = funcExpr->getType();
Steve Naroffae4143e2007-04-26 20:39:23 +0000369 assert(!qType.isNull() && "no type for function call expression");
370
Steve Naroff17f76e02007-05-03 21:03:48 +0000371 const FunctionType *funcT = dyn_cast<FunctionType>(qType.getCanonicalType());
Steve Naroff17f76e02007-05-03 21:03:48 +0000372 assert(funcT && "ParseCallExpr(): not a function type");
Steve Naroffb8c289d2007-05-08 22:18:00 +0000373
Steve Naroff17f76e02007-05-03 21:03:48 +0000374 // If a prototype isn't declared, the parser implicitly defines a func decl
375 QualType resultType = funcT->getResultType();
376
377 if (const FunctionTypeProto *proto = dyn_cast<FunctionTypeProto>(funcT)) {
378 // C99 6.5.2.2p7 - the arguments are implicitly converted, as if by
379 // assignment, to the types of the corresponding parameter, ...
380
381 unsigned NumArgsInProto = proto->getNumArgs();
Steve Naroffb8c289d2007-05-08 22:18:00 +0000382 unsigned NumArgsToCheck = NumArgsInCall;
Steve Naroff17f76e02007-05-03 21:03:48 +0000383
Steve Naroffb8c289d2007-05-08 22:18:00 +0000384 if (NumArgsInCall < NumArgsInProto)
Steve Naroff56faab22007-05-30 04:20:12 +0000385 Diag(RParenLoc, diag::err_typecheck_call_too_few_args,
Steve Naroffeb9da942007-05-30 00:06:37 +0000386 funcExpr->getSourceRange());
Steve Naroffb8c289d2007-05-08 22:18:00 +0000387 else if (NumArgsInCall > NumArgsInProto) {
Steve Naroff8563f652007-05-28 19:25:56 +0000388 if (!proto->isVariadic()) {
Steve Naroff56faab22007-05-30 04:20:12 +0000389 Diag(((Expr **)Args)[NumArgsInProto+1]->getLocStart(),
390 diag::err_typecheck_call_too_many_args, funcExpr->getSourceRange(),
391 ((Expr **)Args)[NumArgsInProto+1]->getSourceRange());
Steve Naroff8563f652007-05-28 19:25:56 +0000392 }
Steve Naroffb8c289d2007-05-08 22:18:00 +0000393 NumArgsToCheck = NumArgsInProto;
Steve Naroff17f76e02007-05-03 21:03:48 +0000394 }
395 // Continue to check argument types (even if we have too few/many args).
Steve Naroffb8c289d2007-05-08 22:18:00 +0000396 for (unsigned i = 0; i < NumArgsToCheck; i++) {
Steve Naroff8563f652007-05-28 19:25:56 +0000397 Expr *argExpr = ((Expr **)Args)[i];
Steve Naroff8563f652007-05-28 19:25:56 +0000398 assert(argExpr && "ParseCallExpr(): missing argument expression");
399
Steve Naroff17f76e02007-05-03 21:03:48 +0000400 QualType lhsType = proto->getArgType(i);
Steve Naroff8563f652007-05-28 19:25:56 +0000401 QualType rhsType = argExpr->getType();
Steve Naroff17f76e02007-05-03 21:03:48 +0000402
403 if (lhsType == rhsType) // common case, fast path...
404 continue;
405 AssignmentConversionResult result;
Steve Naroff218bc2b2007-05-04 21:54:46 +0000406 UsualAssignmentConversions(lhsType, rhsType, result);
Steve Naroff17f76e02007-05-03 21:03:48 +0000407
Steve Naroff6f49f5d2007-05-29 14:23:36 +0000408 SourceLocation l = argExpr->getLocStart();
Steve Naroff17f76e02007-05-03 21:03:48 +0000409
410 // decode the result (notice that AST's are still created for extensions).
Steve Naroff17f76e02007-05-03 21:03:48 +0000411 switch (result) {
412 case Compatible:
413 break;
414 case PointerFromInt:
Steve Naroff218bc2b2007-05-04 21:54:46 +0000415 // check for null pointer constant (C99 6.3.2.3p3)
Steve Naroffeb9da942007-05-30 00:06:37 +0000416 if (!argExpr->isNullPointerConstant()) {
Steve Naroff56faab22007-05-30 04:20:12 +0000417 Diag(l, diag::ext_typecheck_passing_pointer_int,
418 lhsType.getAsString(), rhsType.getAsString(),
Steve Naroffeb9da942007-05-30 00:06:37 +0000419 funcExpr->getSourceRange(), argExpr->getSourceRange());
420 }
Steve Naroff17f76e02007-05-03 21:03:48 +0000421 break;
422 case IntFromPointer:
Steve Naroff56faab22007-05-30 04:20:12 +0000423 Diag(l, diag::ext_typecheck_passing_pointer_int,
424 lhsType.getAsString(), rhsType.getAsString(),
Steve Naroffeb9da942007-05-30 00:06:37 +0000425 funcExpr->getSourceRange(), argExpr->getSourceRange());
Steve Naroff17f76e02007-05-03 21:03:48 +0000426 break;
427 case IncompatiblePointer:
Steve Naroff8563f652007-05-28 19:25:56 +0000428 Diag(l, diag::ext_typecheck_passing_incompatible_pointer,
Steve Naroffeb9da942007-05-30 00:06:37 +0000429 rhsType.getAsString(), lhsType.getAsString(),
430 funcExpr->getSourceRange(), argExpr->getSourceRange());
Steve Naroff17f76e02007-05-03 21:03:48 +0000431 break;
Steve Naroff1f4d7272007-05-11 04:00:31 +0000432 case CompatiblePointerDiscardsQualifiers:
Steve Naroffeb9da942007-05-30 00:06:37 +0000433 Diag(l, diag::ext_typecheck_passing_discards_qualifiers,
434 rhsType.getAsString(), lhsType.getAsString(),
435 funcExpr->getSourceRange(), argExpr->getSourceRange());
Steve Naroff1f4d7272007-05-11 04:00:31 +0000436 break;
Steve Naroff17f76e02007-05-03 21:03:48 +0000437 case Incompatible:
Steve Naroff8563f652007-05-28 19:25:56 +0000438 return Diag(l, diag::err_typecheck_passing_incompatible,
439 rhsType.getAsString(), lhsType.getAsString(),
440 funcExpr->getSourceRange(), argExpr->getSourceRange());
Steve Naroff17f76e02007-05-03 21:03:48 +0000441 }
442 }
Steve Naroffb8c289d2007-05-08 22:18:00 +0000443 // Even if the types checked, bail if we had the wrong number of arguments.
444 if ((NumArgsInCall != NumArgsInProto) && !proto->isVariadic())
445 return true;
Steve Naroffae4143e2007-04-26 20:39:23 +0000446 }
Steve Naroff509fe022007-05-17 01:16:00 +0000447 return new CallExpr((Expr*)Fn, (Expr**)Args, NumArgsInCall, resultType,
448 RParenLoc);
Chris Lattnere168f762006-11-10 05:29:30 +0000449}
450
451Action::ExprResult Sema::
452ParseCastExpr(SourceLocation LParenLoc, TypeTy *Ty,
453 SourceLocation RParenLoc, ExprTy *Op) {
Chris Lattner0d8b1a12006-11-20 04:34:45 +0000454 // If error parsing type, ignore.
Steve Naroffae4143e2007-04-26 20:39:23 +0000455 assert((Ty != 0) && "ParseCastExpr(): missing type");
Steve Naroff509fe022007-05-17 01:16:00 +0000456 return new CastExpr(QualType::getFromOpaquePtr(Ty), (Expr*)Op, LParenLoc);
Chris Lattnere168f762006-11-10 05:29:30 +0000457}
458
Steve Narofff8a28c52007-05-15 20:29:32 +0000459inline QualType Sema::CheckConditionalOperands( // C99 6.5.15
460 Expr *Cond, Expr *LHS, Expr *RHS, SourceLocation questionLoc) {
Steve Naroffa78fe7e2007-05-16 19:47:19 +0000461 QualType cond = Cond->getType();
462 QualType lhs = LHS->getType();
463 QualType rhs = RHS->getType();
464
Steve Narofff8a28c52007-05-15 20:29:32 +0000465 assert(!cond.isNull() && "ParseConditionalOp(): no conditional type");
466 assert(!lhs.isNull() && "ParseConditionalOp(): no lhs type");
467 assert(!rhs.isNull() && "ParseConditionalOp(): no rhs type");
468
Steve Naroffa78fe7e2007-05-16 19:47:19 +0000469 cond = UsualUnaryConversion(cond);
470 lhs = UsualUnaryConversion(lhs);
471 rhs = UsualUnaryConversion(rhs);
472
473 // first, check the condition.
474 if (!cond->isScalarType()) { // C99 6.5.15p2
Steve Naroff53f07dc2007-05-17 21:49:33 +0000475 Diag(Cond->getLocStart(), diag::err_typecheck_cond_expect_scalar,
Steve Naroff509fe022007-05-17 01:16:00 +0000476 cond.getAsString());
Steve Naroffa78fe7e2007-05-16 19:47:19 +0000477 return QualType();
478 }
479 // now check the two expressions.
480 if (lhs->isArithmeticType() && rhs->isArithmeticType()) // C99 6.5.15p3,5
481 return UsualArithmeticConversions(lhs, rhs);
482
483 if ((lhs->isStructureType() && rhs->isStructureType()) || // C99 6.5.15p3
484 (lhs->isUnionType() && rhs->isUnionType())) {
485 TagType *lTag = cast<TagType>(lhs.getCanonicalType());
486 TagType *rTag = cast<TagType>(rhs.getCanonicalType());
487
488 if (lTag->getDecl()->getIdentifier() == rTag->getDecl()->getIdentifier())
489 return lhs;
490 else {
491 Diag(questionLoc, diag::err_typecheck_cond_incompatible_operands,
Steve Naroff3c87a572007-05-28 19:40:22 +0000492 lhs.getAsString(), rhs.getAsString(),
493 LHS->getSourceRange(), RHS->getSourceRange());
Steve Naroffa78fe7e2007-05-16 19:47:19 +0000494 return QualType();
495 }
496 }
Steve Naroff30d1fbc2007-05-20 19:46:53 +0000497 if (lhs->isPointerType() && RHS->isNullPointerConstant()) // C99 6.5.15p3
498 return lhs;
499 if (rhs->isPointerType() && LHS->isNullPointerConstant())
500 return rhs;
501
Steve Naroffa78fe7e2007-05-16 19:47:19 +0000502 if (lhs->isPointerType() && rhs->isPointerType()) { // C99 6.5.15p3,6
503 QualType lhptee, rhptee;
504
505 // get the "pointed to" type
506 lhptee = cast<PointerType>(lhs.getCanonicalType())->getPointeeType();
507 rhptee = cast<PointerType>(rhs.getCanonicalType())->getPointeeType();
508
509 // ignore qualifiers on void (C99 6.5.15p3, clause 6)
510 if (lhptee.getUnqualifiedType()->isVoidType() &&
511 (rhptee->isObjectType() || rhptee->isIncompleteType()))
512 return lhs;
513 if (rhptee.getUnqualifiedType()->isVoidType() &&
514 (lhptee->isObjectType() || lhptee->isIncompleteType()))
515 return rhs;
516
517 // FIXME: C99 6.5.15p6: If both operands are pointers to compatible types
518 // *or* to differently qualified versions of compatible types, the result
519 // type is a pointer to an appropriately qualified version of the
520 // *composite* type.
521 if (!Type::typesAreCompatible(lhptee.getUnqualifiedType(),
522 rhptee.getUnqualifiedType())) {
523 Diag(questionLoc, diag::ext_typecheck_cond_incompatible_pointers,
Steve Naroffeb9da942007-05-30 00:06:37 +0000524 lhs.getAsString(), rhs.getAsString(),
525 LHS->getSourceRange(), RHS->getSourceRange());
Steve Naroffa78fe7e2007-05-16 19:47:19 +0000526 return lhs; // FIXME: this is an _ext - is this return o.k?
527 }
528 }
529 if (lhs->isVoidType() && rhs->isVoidType()) // C99 6.5.15p3
530 return lhs;
Steve Naroffa78fe7e2007-05-16 19:47:19 +0000531
532 Diag(questionLoc, diag::err_typecheck_cond_incompatible_operands,
Steve Naroffeb9da942007-05-30 00:06:37 +0000533 lhs.getAsString(), rhs.getAsString(),
534 LHS->getSourceRange(), RHS->getSourceRange());
Steve Naroffa78fe7e2007-05-16 19:47:19 +0000535 return QualType();
Steve Narofff8a28c52007-05-15 20:29:32 +0000536}
537
Chris Lattnere168f762006-11-10 05:29:30 +0000538/// ParseConditionalOp - Parse a ?: operation. Note that 'LHS' may be null
539/// in the case of a the GNU conditional expr extension.
540Action::ExprResult Sema::ParseConditionalOp(SourceLocation QuestionLoc,
541 SourceLocation ColonLoc,
542 ExprTy *Cond, ExprTy *LHS,
543 ExprTy *RHS) {
Steve Narofff8a28c52007-05-15 20:29:32 +0000544 QualType result = CheckConditionalOperands((Expr *)Cond, (Expr *)LHS,
545 (Expr *)RHS, QuestionLoc);
546 if (result.isNull())
547 return true;
548 return new ConditionalOperator((Expr*)Cond, (Expr*)LHS, (Expr*)RHS, result);
Chris Lattnere168f762006-11-10 05:29:30 +0000549}
550
Steve Naroff1926c832007-04-24 00:23:05 +0000551/// UsualUnaryConversion - Performs various conversions that are common to most
552/// operators (C99 6.3). The conversions of array and function types are
553/// sometimes surpressed. For example, the array->pointer conversion doesn't
554/// apply if the array is an argument to the sizeof or address (&) operators.
555/// In these instances, this routine should *not* be called.
556QualType Sema::UsualUnaryConversion(QualType t) {
557 assert(!t.isNull() && "UsualUnaryConversion - missing type");
Steve Naroff4b7ce032007-04-20 22:26:17 +0000558
Steve Naroff1926c832007-04-24 00:23:05 +0000559 if (t->isPromotableIntegerType()) // C99 6.3.1.1p2
560 return Context.IntTy;
Steve Naroffa78fe7e2007-05-16 19:47:19 +0000561 if (t->isFunctionType()) // C99 6.3.2.1p4
562 return Context.getPointerType(t.getCanonicalType());
563 if (const ArrayType *ary = dyn_cast<ArrayType>(t.getCanonicalType()))
564 return Context.getPointerType(ary->getElementType()); // C99 6.3.2.1p3
Steve Naroff1926c832007-04-24 00:23:05 +0000565 return t;
566}
567
568/// UsualArithmeticConversions - Performs various conversions that are common to
569/// binary operators (C99 6.3.1.8). If both operands aren't arithmetic, this
570/// routine returns the first non-arithmetic type found. The client is
571/// responsible for emitting appropriate error diagnostics.
572QualType Sema::UsualArithmeticConversions(QualType t1, QualType t2) {
Steve Naroffb01bbe32007-04-25 01:22:31 +0000573 QualType lhs = UsualUnaryConversion(t1);
574 QualType rhs = UsualUnaryConversion(t2);
Steve Naroff1926c832007-04-24 00:23:05 +0000575
576 // if either operand is not of arithmetic type, no conversion is possible.
Steve Naroffb01bbe32007-04-25 01:22:31 +0000577 if (!lhs->isArithmeticType())
578 return lhs;
Steve Narofff633d092007-04-25 19:01:39 +0000579 if (!rhs->isArithmeticType())
Steve Naroffb01bbe32007-04-25 01:22:31 +0000580 return rhs;
Steve Naroff1926c832007-04-24 00:23:05 +0000581
Steve Narofff633d092007-04-25 19:01:39 +0000582 // if both arithmetic types are identical, no conversion is needed.
Steve Naroffb01bbe32007-04-25 01:22:31 +0000583 if (lhs == rhs)
584 return lhs;
Steve Naroff1926c832007-04-24 00:23:05 +0000585
Steve Naroffb01bbe32007-04-25 01:22:31 +0000586 // at this point, we have two different arithmetic types.
587
588 // Handle complex types first (C99 6.3.1.8p1).
589 if (lhs->isComplexType() || rhs->isComplexType()) {
590 // if we have an integer operand, the result is the complex type.
591 if (rhs->isIntegerType())
592 return lhs;
593 if (lhs->isIntegerType())
594 return rhs;
595
Steve Naroffe4718892007-04-27 18:30:00 +0000596 return Context.maxComplexType(lhs, rhs);
Steve Naroffbf223ba2007-04-24 20:56:26 +0000597 }
Steve Naroffb01bbe32007-04-25 01:22:31 +0000598 // Now handle "real" floating types (i.e. float, double, long double).
599 if (lhs->isRealFloatingType() || rhs->isRealFloatingType()) {
600 // if we have an integer operand, the result is the real floating type.
601 if (rhs->isIntegerType())
602 return lhs;
603 if (lhs->isIntegerType())
604 return rhs;
605
606 // we have two real floating types, float/complex combos were handled above.
Steve Naroffe4718892007-04-27 18:30:00 +0000607 return Context.maxFloatingType(lhs, rhs);
Steve Naroffb01bbe32007-04-25 01:22:31 +0000608 }
Steve Naroffe4718892007-04-27 18:30:00 +0000609 return Context.maxIntegerType(lhs, rhs);
Steve Narofff1e53692007-03-23 22:27:02 +0000610}
611
Steve Naroff3f597292007-05-11 22:18:03 +0000612// CheckPointerTypesForAssignment - This is a very tricky routine (despite
613// being closely modeled after the C99 spec:-). The odd characteristic of this
614// routine is it effectively iqnores the qualifiers on the top level pointee.
615// This circumvents the usual type rules specified in 6.2.7p1 & 6.7.5.[1-3].
616// FIXME: add a couple examples in this comment.
617QualType Sema::CheckPointerTypesForAssignment(QualType lhsType,
618 QualType rhsType,
619 AssignmentConversionResult &r) {
620 QualType lhptee, rhptee;
Steve Naroff1f4d7272007-05-11 04:00:31 +0000621
622 // get the "pointed to" type (ignoring qualifiers at the top level)
Steve Naroff3f597292007-05-11 22:18:03 +0000623 lhptee = cast<PointerType>(lhsType.getCanonicalType())->getPointeeType();
624 rhptee = cast<PointerType>(rhsType.getCanonicalType())->getPointeeType();
Steve Naroff1f4d7272007-05-11 04:00:31 +0000625
626 // make sure we operate on the canonical type
Steve Naroff3f597292007-05-11 22:18:03 +0000627 lhptee = lhptee.getCanonicalType();
628 rhptee = rhptee.getCanonicalType();
Steve Naroff1f4d7272007-05-11 04:00:31 +0000629
Steve Naroff3f597292007-05-11 22:18:03 +0000630 // C99 6.5.16.1p1: This following citation is common to constraints
631 // 3 & 4 (below). ...and the type *pointed to* by the left has all the
632 // qualifiers of the type *pointed to* by the right;
633 if ((lhptee.getQualifiers() & rhptee.getQualifiers()) !=
634 rhptee.getQualifiers())
635 r = CompatiblePointerDiscardsQualifiers;
636
637 // C99 6.5.16.1p1 (constraint 4): If one operand is a pointer to an object or
638 // incomplete type and the other is a pointer to a qualified or unqualified
639 // version of void...
640 if (lhptee.getUnqualifiedType()->isVoidType() &&
641 (rhptee->isObjectType() || rhptee->isIncompleteType()))
642 ;
643 else if (rhptee.getUnqualifiedType()->isVoidType() &&
644 (lhptee->isObjectType() || lhptee->isIncompleteType()))
645 ;
646 // C99 6.5.16.1p1 (constraint 3): both operands are pointers to qualified or
647 // unqualified versions of compatible types, ...
648 else if (!Type::typesAreCompatible(lhptee.getUnqualifiedType(),
649 rhptee.getUnqualifiedType()))
650 r = IncompatiblePointer; // this "trumps" PointerAssignDiscardsQualifiers
651 return rhsType;
Steve Naroff1f4d7272007-05-11 04:00:31 +0000652}
653
Steve Naroff17f76e02007-05-03 21:03:48 +0000654/// UsualAssignmentConversions (C99 6.5.16) - This routine currently
655/// has code to accommodate several GCC extensions when type checking
656/// pointers. Here are some objectionable examples that GCC considers warnings:
657///
658/// int a, *pint;
659/// short *pshort;
660/// struct foo *pfoo;
661///
662/// pint = pshort; // warning: assignment from incompatible pointer type
663/// a = pint; // warning: assignment makes integer from pointer without a cast
664/// pint = a; // warning: assignment makes pointer from integer without a cast
665/// pint = pfoo; // warning: assignment from incompatible pointer type
666///
667/// As a result, the code for dealing with pointers is more complex than the
668/// C99 spec dictates.
669/// Note: the warning above turn into errors when -pedantic-errors is enabled.
670///
Steve Naroff218bc2b2007-05-04 21:54:46 +0000671QualType Sema::UsualAssignmentConversions(QualType lhsType, QualType rhsType,
Steve Naroff17f76e02007-05-03 21:03:48 +0000672 AssignmentConversionResult &r) {
Bill Wendling216423b2007-05-30 06:30:29 +0000673 // This check seems unnatural, however it is necessary to insure the proper
674 // conversion of functions/arrays. If the conversion were done for all
675 // DeclExpr's (created by ParseIdentifierExpr), it would mess up the unary
676 // expressions that surpress this implicit conversion (&, sizeof).
Steve Naroffb891de32007-05-02 23:51:10 +0000677 if (rhsType->isFunctionType() || rhsType->isArrayType())
678 rhsType = UsualUnaryConversion(rhsType);
679
Steve Naroff17f76e02007-05-03 21:03:48 +0000680 r = Compatible;
Steve Naroff9eb24652007-05-02 21:58:15 +0000681 if (lhsType->isArithmeticType() && rhsType->isArithmeticType())
682 return lhsType;
683 else if (lhsType->isPointerType()) {
684 if (rhsType->isIntegerType()) {
Steve Naroff218bc2b2007-05-04 21:54:46 +0000685 r = PointerFromInt;
Steve Naroffb891de32007-05-02 23:51:10 +0000686 return rhsType;
Steve Naroff9eb24652007-05-02 21:58:15 +0000687 }
Steve Naroff3f597292007-05-11 22:18:03 +0000688 if (rhsType->isPointerType())
689 return CheckPointerTypesForAssignment(lhsType, rhsType, r);
Steve Naroff9eb24652007-05-02 21:58:15 +0000690 } else if (rhsType->isPointerType()) {
691 if (lhsType->isIntegerType()) {
692 // C99 6.5.16.1p1: the left operand is _Bool and the right is a pointer.
693 if (lhsType != Context.BoolTy)
Steve Naroff17f76e02007-05-03 21:03:48 +0000694 r = IntFromPointer;
Steve Naroffb891de32007-05-02 23:51:10 +0000695 return rhsType;
Steve Naroff9eb24652007-05-02 21:58:15 +0000696 }
Steve Naroff3f597292007-05-11 22:18:03 +0000697 if (lhsType->isPointerType())
698 return CheckPointerTypesForAssignment(lhsType, rhsType, r);
Steve Naroff1f4d7272007-05-11 04:00:31 +0000699 } else if (isa<TagType>(lhsType) && isa<TagType>(rhsType)) {
700 if (Type::tagTypesAreCompatible(lhsType, rhsType))
Steve Naroffb891de32007-05-02 23:51:10 +0000701 return rhsType;
Bill Wendling216423b2007-05-30 06:30:29 +0000702 }
Steve Naroff17f76e02007-05-03 21:03:48 +0000703 r = Incompatible;
704 return QualType();
Steve Naroff9eb24652007-05-02 21:58:15 +0000705}
706
Steve Naroff6f49f5d2007-05-29 14:23:36 +0000707inline void Sema::InvalidOperands(SourceLocation loc, Expr *lex, Expr *rex) {
708 Diag(loc, diag::err_typecheck_invalid_operands,
709 lex->getType().getAsString(), rex->getType().getAsString(),
710 lex->getSourceRange(), rex->getSourceRange());
711}
712
Steve Naroff218bc2b2007-05-04 21:54:46 +0000713inline QualType Sema::CheckMultiplyDivideOperands(
714 Expr *lex, Expr *rex, SourceLocation loc)
Steve Naroff5c10d4b2007-04-20 23:42:24 +0000715{
Steve Naroff1926c832007-04-24 00:23:05 +0000716 QualType resType = UsualArithmeticConversions(lex->getType(), rex->getType());
Steve Naroff5c10d4b2007-04-20 23:42:24 +0000717
Steve Naroff218bc2b2007-05-04 21:54:46 +0000718 if (resType->isArithmeticType())
719 return resType;
Steve Naroff6f49f5d2007-05-29 14:23:36 +0000720 InvalidOperands(loc, lex, rex);
Steve Naroff218bc2b2007-05-04 21:54:46 +0000721 return QualType();
Steve Naroff26c8ea52007-03-21 21:08:52 +0000722}
723
Steve Naroff218bc2b2007-05-04 21:54:46 +0000724inline QualType Sema::CheckRemainderOperands(
725 Expr *lex, Expr *rex, SourceLocation loc)
726{
727 QualType resType = UsualArithmeticConversions(lex->getType(), rex->getType());
728
729 if (resType->isIntegerType())
730 return resType;
Steve Naroff6f49f5d2007-05-29 14:23:36 +0000731 InvalidOperands(loc, lex, rex);
Steve Naroff218bc2b2007-05-04 21:54:46 +0000732 return QualType();
733}
734
735inline QualType Sema::CheckAdditionOperands( // C99 6.5.6
736 Expr *lex, Expr *rex, SourceLocation loc)
Steve Naroff1926c832007-04-24 00:23:05 +0000737{
Steve Naroffe4718892007-04-27 18:30:00 +0000738 QualType lhsType = lex->getType(), rhsType = rex->getType();
739 QualType resType = UsualArithmeticConversions(lhsType, rhsType);
740
741 // handle the common case first (both operands are arithmetic).
742 if (resType->isArithmeticType())
Steve Naroff218bc2b2007-05-04 21:54:46 +0000743 return resType;
744
745 if ((lhsType->isPointerType() && rhsType->isIntegerType()) ||
746 (lhsType->isIntegerType() && rhsType->isPointerType()))
747 return resType;
Steve Naroff6f49f5d2007-05-29 14:23:36 +0000748 InvalidOperands(loc, lex, rex);
Steve Naroff218bc2b2007-05-04 21:54:46 +0000749 return QualType();
Steve Naroff26c8ea52007-03-21 21:08:52 +0000750}
751
Steve Naroff218bc2b2007-05-04 21:54:46 +0000752inline QualType Sema::CheckSubtractionOperands( // C99 6.5.6
753 Expr *lex, Expr *rex, SourceLocation loc)
754{
755 QualType lhsType = lex->getType(), rhsType = rex->getType();
756 QualType resType = UsualArithmeticConversions(lhsType, rhsType);
757
758 // handle the common case first (both operands are arithmetic).
759 if (resType->isArithmeticType())
760 return resType;
761 if ((lhsType->isPointerType() && rhsType->isIntegerType()) ||
762 (lhsType->isPointerType() && rhsType->isPointerType()))
763 return resType;
Steve Naroff6f49f5d2007-05-29 14:23:36 +0000764 InvalidOperands(loc, lex, rex);
Steve Naroff218bc2b2007-05-04 21:54:46 +0000765 return QualType();
766}
767
768inline QualType Sema::CheckShiftOperands( // C99 6.5.7
769 Expr *lex, Expr *rex, SourceLocation loc)
Steve Naroff1926c832007-04-24 00:23:05 +0000770{
771 QualType resType = UsualArithmeticConversions(lex->getType(), rex->getType());
772
Steve Naroff218bc2b2007-05-04 21:54:46 +0000773 if (resType->isIntegerType())
774 return resType;
Steve Naroff6f49f5d2007-05-29 14:23:36 +0000775 InvalidOperands(loc, lex, rex);
Steve Naroff218bc2b2007-05-04 21:54:46 +0000776 return QualType();
Steve Naroff26c8ea52007-03-21 21:08:52 +0000777}
778
Steve Naroff218bc2b2007-05-04 21:54:46 +0000779inline QualType Sema::CheckRelationalOperands( // C99 6.5.8
780 Expr *lex, Expr *rex, SourceLocation loc)
Steve Naroff1926c832007-04-24 00:23:05 +0000781{
782 QualType lType = lex->getType(), rType = rex->getType();
783
784 if (lType->isRealType() && rType->isRealType())
Steve Naroff218bc2b2007-05-04 21:54:46 +0000785 return Context.IntTy;
Steve Naroffe4718892007-04-27 18:30:00 +0000786
787 if (lType->isPointerType() && rType->isPointerType())
Steve Naroff218bc2b2007-05-04 21:54:46 +0000788 return Context.IntTy;
Steve Naroffe4718892007-04-27 18:30:00 +0000789
Steve Naroff043d45d2007-05-15 02:32:35 +0000790 if (lType->isIntegerType() || rType->isIntegerType()) { // GCC extension.
Steve Naroff218bc2b2007-05-04 21:54:46 +0000791 Diag(loc, diag::ext_typecheck_comparison_of_pointer_integer);
Steve Naroff043d45d2007-05-15 02:32:35 +0000792 return Context.IntTy;
793 }
Steve Naroff6f49f5d2007-05-29 14:23:36 +0000794 InvalidOperands(loc, lex, rex);
Steve Naroff218bc2b2007-05-04 21:54:46 +0000795 return QualType();
Steve Naroff26c8ea52007-03-21 21:08:52 +0000796}
797
Steve Naroff218bc2b2007-05-04 21:54:46 +0000798inline QualType Sema::CheckEqualityOperands( // C99 6.5.9
799 Expr *lex, Expr *rex, SourceLocation loc)
Steve Naroff1926c832007-04-24 00:23:05 +0000800{
801 QualType lType = lex->getType(), rType = rex->getType();
802
803 if (lType->isArithmeticType() && rType->isArithmeticType())
Steve Naroff218bc2b2007-05-04 21:54:46 +0000804 return Context.IntTy;
Steve Naroffe4718892007-04-27 18:30:00 +0000805 if (lType->isPointerType() && rType->isPointerType())
Steve Naroff218bc2b2007-05-04 21:54:46 +0000806 return Context.IntTy;
807
Steve Naroff043d45d2007-05-15 02:32:35 +0000808 if (lType->isIntegerType() || rType->isIntegerType()) { // GCC extension.
Steve Naroff218bc2b2007-05-04 21:54:46 +0000809 Diag(loc, diag::ext_typecheck_comparison_of_pointer_integer);
Steve Naroff043d45d2007-05-15 02:32:35 +0000810 return Context.IntTy;
811 }
Steve Naroff6f49f5d2007-05-29 14:23:36 +0000812 InvalidOperands(loc, lex, rex);
Steve Naroff218bc2b2007-05-04 21:54:46 +0000813 return QualType();
Steve Naroff26c8ea52007-03-21 21:08:52 +0000814}
815
Steve Naroff218bc2b2007-05-04 21:54:46 +0000816inline QualType Sema::CheckBitwiseOperands(
817 Expr *lex, Expr *rex, SourceLocation loc)
Steve Naroff1926c832007-04-24 00:23:05 +0000818{
819 QualType resType = UsualArithmeticConversions(lex->getType(), rex->getType());
820
Steve Naroff218bc2b2007-05-04 21:54:46 +0000821 if (resType->isIntegerType())
822 return resType;
Steve Naroff6f49f5d2007-05-29 14:23:36 +0000823 InvalidOperands(loc, lex, rex);
Steve Naroff218bc2b2007-05-04 21:54:46 +0000824 return QualType();
Steve Naroff26c8ea52007-03-21 21:08:52 +0000825}
826
Steve Naroff218bc2b2007-05-04 21:54:46 +0000827inline QualType Sema::CheckLogicalOperands( // C99 6.5.[13,14]
828 Expr *lex, Expr *rex, SourceLocation loc)
Steve Naroff1926c832007-04-24 00:23:05 +0000829{
Steve Naroffe4718892007-04-27 18:30:00 +0000830 QualType lhsType = UsualUnaryConversion(lex->getType());
831 QualType rhsType = UsualUnaryConversion(rex->getType());
832
Steve Naroff218bc2b2007-05-04 21:54:46 +0000833 if (lhsType->isScalarType() || rhsType->isScalarType())
834 return Context.IntTy;
Steve Naroff6f49f5d2007-05-29 14:23:36 +0000835 InvalidOperands(loc, lex, rex);
Steve Naroff218bc2b2007-05-04 21:54:46 +0000836 return QualType();
Steve Naroffae4143e2007-04-26 20:39:23 +0000837}
838
Steve Naroff35d85152007-05-07 00:24:15 +0000839inline QualType Sema::CheckAssignmentOperands( // C99 6.5.16.1
840 Expr *lex, Expr *rex, SourceLocation loc, QualType compoundType)
Steve Naroffae4143e2007-04-26 20:39:23 +0000841{
Steve Naroff0af91202007-04-27 21:51:21 +0000842 QualType lhsType = lex->getType();
Steve Naroff35d85152007-05-07 00:24:15 +0000843 QualType rhsType = compoundType.isNull() ? rex->getType() : compoundType;
Steve Naroff1f4d7272007-05-11 04:00:31 +0000844 bool hadError = false;
Steve Naroff9358c712007-05-27 23:58:33 +0000845 Expr::isModifiableLvalueResult mlval = lex->isModifiableLvalue();
846
847 switch (mlval) { // C99 6.5.16p2
848 case Expr::MLV_Valid:
849 break;
850 case Expr::MLV_ConstQualified:
851 Diag(loc, diag::err_typecheck_assign_const, lex->getSourceRange());
852 hadError = true;
853 break;
854 case Expr::MLV_ArrayType:
855 Diag(loc, diag::err_typecheck_array_not_modifiable_lvalue,
856 lhsType.getAsString(), lex->getSourceRange());
857 return QualType();
858 case Expr::MLV_NotObjectType:
859 Diag(loc, diag::err_typecheck_non_object_not_modifiable_lvalue,
860 lhsType.getAsString(), lex->getSourceRange());
861 return QualType();
862 case Expr::MLV_InvalidExpression:
863 Diag(loc, diag::err_typecheck_expression_not_modifiable_lvalue,
864 lex->getSourceRange());
865 return QualType();
866 case Expr::MLV_IncompleteType:
867 case Expr::MLV_IncompleteVoidType:
868 Diag(loc, diag::err_typecheck_incomplete_type_not_modifiable_lvalue,
869 lhsType.getAsString(), lex->getSourceRange());
870 return QualType();
Steve Naroff218bc2b2007-05-04 21:54:46 +0000871 }
872 if (lhsType == rhsType) // common case, fast path...
873 return lhsType;
874
875 AssignmentConversionResult result;
876 QualType resType = UsualAssignmentConversions(lhsType, rhsType, result);
877
878 // decode the result (notice that extensions still return a type).
Steve Naroffeb9da942007-05-30 00:06:37 +0000879 // FIXME: make sure the text of the message is consistent with function calls.
Steve Naroff218bc2b2007-05-04 21:54:46 +0000880 switch (result) {
881 case Compatible:
Steve Naroff1f4d7272007-05-11 04:00:31 +0000882 break;
Steve Naroff218bc2b2007-05-04 21:54:46 +0000883 case Incompatible:
Steve Naroffe845e272007-05-18 01:06:45 +0000884 Diag(loc, diag::err_typecheck_assign_incompatible,
Chris Lattner84e160a2007-05-19 07:03:17 +0000885 lhsType.getAsString(), rhsType.getAsString(),
886 lex->getSourceRange(), rex->getSourceRange());
Steve Naroff1f4d7272007-05-11 04:00:31 +0000887 hadError = true;
888 break;
Steve Naroff218bc2b2007-05-04 21:54:46 +0000889 case PointerFromInt:
890 // check for null pointer constant (C99 6.3.2.3p3)
Steve Naroff35d85152007-05-07 00:24:15 +0000891 if (compoundType.isNull() && !rex->isNullPointerConstant())
Steve Naroff56faab22007-05-30 04:20:12 +0000892 Diag(loc, diag::ext_typecheck_assign_pointer_int,
893 lhsType.getAsString(), rhsType.getAsString(),
Steve Naroff9358c712007-05-27 23:58:33 +0000894 lex->getSourceRange(), rex->getSourceRange());
Steve Naroff1f4d7272007-05-11 04:00:31 +0000895 break;
Steve Naroff56faab22007-05-30 04:20:12 +0000896 case IntFromPointer:
897 Diag(loc, diag::ext_typecheck_assign_pointer_int,
898 lhsType.getAsString(), rhsType.getAsString(),
Steve Naroff9358c712007-05-27 23:58:33 +0000899 lex->getSourceRange(), rex->getSourceRange());
Steve Naroff1f4d7272007-05-11 04:00:31 +0000900 break;
Steve Naroff218bc2b2007-05-04 21:54:46 +0000901 case IncompatiblePointer:
Steve Naroff9358c712007-05-27 23:58:33 +0000902 Diag(loc, diag::ext_typecheck_assign_incompatible_pointer,
903 lhsType.getAsString(), rhsType.getAsString(),
904 lex->getSourceRange(), rex->getSourceRange());
Steve Naroff1f4d7272007-05-11 04:00:31 +0000905 break;
906 case CompatiblePointerDiscardsQualifiers:
Steve Naroff9358c712007-05-27 23:58:33 +0000907 Diag(loc, diag::ext_typecheck_assign_discards_qualifiers,
908 lhsType.getAsString(), rhsType.getAsString(),
909 lex->getSourceRange(), rex->getSourceRange());
Steve Naroff1f4d7272007-05-11 04:00:31 +0000910 break;
Steve Naroff218bc2b2007-05-04 21:54:46 +0000911 }
Steve Naroff1f4d7272007-05-11 04:00:31 +0000912 return hadError ? QualType() : resType;
Steve Naroffae4143e2007-04-26 20:39:23 +0000913}
914
Steve Naroff218bc2b2007-05-04 21:54:46 +0000915inline QualType Sema::CheckCommaOperands( // C99 6.5.17
Chris Lattner84e160a2007-05-19 07:03:17 +0000916 Expr *lex, Expr *rex, SourceLocation loc) {
Steve Naroff218bc2b2007-05-04 21:54:46 +0000917 return UsualUnaryConversion(rex->getType());
Steve Naroff95af0132007-03-30 23:47:58 +0000918}
919
Steve Naroff71ce2e02007-05-18 22:53:50 +0000920QualType Sema::CheckIncrementDecrementOperand(Expr *op, SourceLocation OpLoc) {
Steve Naroff094046f2007-05-13 03:21:25 +0000921 QualType resType = UsualArithmeticConversions(op->getType(), Context.IntTy);
Steve Naroff35d85152007-05-07 00:24:15 +0000922 assert(!resType.isNull() && "no type for increment/decrement expression");
Steve Naroffd50c88e2007-04-05 21:15:20 +0000923
Steve Naroff46ba1eb2007-04-03 23:13:13 +0000924 // C99 6.5.2.4p1
Steve Naroff35d85152007-05-07 00:24:15 +0000925 if (const PointerType *pt = dyn_cast<PointerType>(resType)) {
926 if (!pt->getPointeeType()->isObjectType()) { // C99 6.5.2.4p2, 6.5.6p2
Steve Naroff71ce2e02007-05-18 22:53:50 +0000927 Diag(OpLoc, diag::err_typecheck_arithmetic_incomplete_type,
928 resType.getAsString(), op->getSourceRange());
Steve Naroff35d85152007-05-07 00:24:15 +0000929 return QualType();
930 }
931 } else if (!resType->isRealType()) {
Steve Naroff95af0132007-03-30 23:47:58 +0000932 // FIXME: Allow Complex as a GCC extension.
Steve Naroff71ce2e02007-05-18 22:53:50 +0000933 Diag(OpLoc, diag::err_typecheck_illegal_increment_decrement,
934 resType.getAsString(), op->getSourceRange());
Steve Naroff35d85152007-05-07 00:24:15 +0000935 return QualType();
Steve Naroff46ba1eb2007-04-03 23:13:13 +0000936 }
Steve Naroff094046f2007-05-13 03:21:25 +0000937 // At this point, we know we have a real or pointer type. Now make sure
938 // the operand is a modifiable lvalue.
Steve Naroff9358c712007-05-27 23:58:33 +0000939 Expr::isModifiableLvalueResult mlval = op->isModifiableLvalue();
940 if (mlval != Expr::MLV_Valid) {
941 // FIXME: emit a more precise diagnostic...
Steve Naroff71ce2e02007-05-18 22:53:50 +0000942 Diag(OpLoc, diag::err_typecheck_invalid_lvalue_incr_decr,
Chris Lattner84e160a2007-05-19 07:03:17 +0000943 op->getSourceRange());
Steve Naroff35d85152007-05-07 00:24:15 +0000944 return QualType();
945 }
946 return resType;
Steve Naroff26c8ea52007-03-21 21:08:52 +0000947}
948
Steve Naroff1926c832007-04-24 00:23:05 +0000949/// getPrimaryDeclaration - Helper function for CheckAddressOfOperand().
Steve Naroff47500512007-04-19 23:00:49 +0000950/// This routine allows us to typecheck complex/recursive expressions
951/// where the declaration is needed for type checking. Here are some
952/// examples: &s.xx, &s.zz[1].yy, &(1+2), &(XX), &"123"[2].
Steve Naroff1926c832007-04-24 00:23:05 +0000953static Decl *getPrimaryDeclaration(Expr *e) {
Steve Naroff47500512007-04-19 23:00:49 +0000954 switch (e->getStmtClass()) {
955 case Stmt::DeclRefExprClass:
956 return cast<DeclRefExpr>(e)->getDecl();
957 case Stmt::MemberExprClass:
Steve Naroff1926c832007-04-24 00:23:05 +0000958 return getPrimaryDeclaration(cast<MemberExpr>(e)->getBase());
Steve Naroff47500512007-04-19 23:00:49 +0000959 case Stmt::ArraySubscriptExprClass:
Steve Naroff1926c832007-04-24 00:23:05 +0000960 return getPrimaryDeclaration(cast<ArraySubscriptExpr>(e)->getBase());
Steve Naroff47500512007-04-19 23:00:49 +0000961 case Stmt::CallExprClass:
Steve Naroff1926c832007-04-24 00:23:05 +0000962 return getPrimaryDeclaration(cast<CallExpr>(e)->getCallee());
Steve Naroff47500512007-04-19 23:00:49 +0000963 case Stmt::UnaryOperatorClass:
Steve Naroff1926c832007-04-24 00:23:05 +0000964 return getPrimaryDeclaration(cast<UnaryOperator>(e)->getSubExpr());
Steve Naroff47500512007-04-19 23:00:49 +0000965 case Stmt::ParenExprClass:
Steve Naroff1926c832007-04-24 00:23:05 +0000966 return getPrimaryDeclaration(cast<ParenExpr>(e)->getSubExpr());
Steve Naroff47500512007-04-19 23:00:49 +0000967 default:
968 return 0;
969 }
970}
971
972/// CheckAddressOfOperand - The operand of & must be either a function
973/// designator or an lvalue designating an object. If it is an lvalue, the
974/// object cannot be declared with storage class register or be a bit field.
975/// Note: The usual conversions are *not* applied to the operand of the &
Steve Naroffa78fe7e2007-05-16 19:47:19 +0000976/// operator (C99 6.3.2.1p[2-4]), and its result is never an lvalue.
Steve Naroff35d85152007-05-07 00:24:15 +0000977QualType Sema::CheckAddressOfOperand(Expr *op, SourceLocation OpLoc) {
Steve Naroff1926c832007-04-24 00:23:05 +0000978 Decl *dcl = getPrimaryDeclaration(op);
Steve Naroff9358c712007-05-27 23:58:33 +0000979 Expr::isLvalueResult lval = op->isLvalue();
Steve Naroff47500512007-04-19 23:00:49 +0000980
Steve Naroff9358c712007-05-27 23:58:33 +0000981 if (lval != Expr::LV_Valid) { // C99 6.5.3.2p1
Steve Naroff5dd642e2007-05-14 18:14:51 +0000982 if (dcl && isa<FunctionDecl>(dcl)) // allow function designators
983 ;
Steve Naroff9358c712007-05-27 23:58:33 +0000984 else { // FIXME: emit more specific diag...
Steve Naroff71ce2e02007-05-18 22:53:50 +0000985 Diag(OpLoc, diag::err_typecheck_invalid_lvalue_addrof,
986 op->getSourceRange());
Steve Naroff35d85152007-05-07 00:24:15 +0000987 return QualType();
988 }
Steve Naroff47500512007-04-19 23:00:49 +0000989 } else if (dcl) {
990 // We have an lvalue with a decl. Make sure the decl is not declared
991 // with the register storage-class specifier.
992 if (const VarDecl *vd = dyn_cast<VarDecl>(dcl)) {
Steve Naroff35d85152007-05-07 00:24:15 +0000993 if (vd->getStorageClass() == VarDecl::Register) {
Steve Naroff71ce2e02007-05-18 22:53:50 +0000994 Diag(OpLoc, diag::err_typecheck_address_of_register,
Chris Lattner84e160a2007-05-19 07:03:17 +0000995 op->getSourceRange());
Steve Naroff35d85152007-05-07 00:24:15 +0000996 return QualType();
997 }
Steve Narofff633d092007-04-25 19:01:39 +0000998 } else
999 assert(0 && "Unknown/unexpected decl type");
1000
Steve Naroff47500512007-04-19 23:00:49 +00001001 // FIXME: add check for bitfields!
1002 }
1003 // If the operand has type "type", the result has type "pointer to type".
Steve Naroff35d85152007-05-07 00:24:15 +00001004 return Context.getPointerType(op->getType());
Steve Naroff47500512007-04-19 23:00:49 +00001005}
1006
Steve Naroff35d85152007-05-07 00:24:15 +00001007QualType Sema::CheckIndirectionOperand(Expr *op, SourceLocation OpLoc) {
1008 QualType qType = UsualUnaryConversion(op->getType());
1009
Steve Naroff47500512007-04-19 23:00:49 +00001010 assert(!qType.isNull() && "no type for * expression");
1011
Steve Naroff758ada12007-05-28 16:15:57 +00001012 if (PointerType *PT = dyn_cast<PointerType>(qType.getCanonicalType())) {
1013 QualType ptype = PT->getPointeeType();
1014 // C99 6.5.3.2p4. "if it points to an object,...".
1015 if (ptype->isIncompleteType()) { // An incomplete type is not an object
1016 // GCC compat: special case 'void *' (treat as warning).
1017 if (ptype->isVoidType()) {
1018 Diag(OpLoc, diag::ext_typecheck_deref_ptr_to_void,
Steve Naroffeb9da942007-05-30 00:06:37 +00001019 qType.getAsString(), op->getSourceRange());
Steve Naroff758ada12007-05-28 16:15:57 +00001020 } else {
1021 Diag(OpLoc, diag::err_typecheck_deref_incomplete_type,
Steve Naroffeb9da942007-05-30 00:06:37 +00001022 ptype.getAsString(), op->getSourceRange());
Steve Naroff758ada12007-05-28 16:15:57 +00001023 return QualType();
1024 }
1025 }
1026 return ptype;
1027 }
1028 Diag(OpLoc, diag::err_typecheck_indirection_requires_pointer,
Steve Naroffeb9da942007-05-30 00:06:37 +00001029 qType.getAsString(), op->getSourceRange());
Steve Naroff35d85152007-05-07 00:24:15 +00001030 return QualType();
Steve Naroff1926c832007-04-24 00:23:05 +00001031}
Steve Naroff218bc2b2007-05-04 21:54:46 +00001032
1033static inline BinaryOperator::Opcode ConvertTokenKindToBinaryOpcode(
1034 tok::TokenKind Kind) {
1035 BinaryOperator::Opcode Opc;
1036 switch (Kind) {
1037 default: assert(0 && "Unknown binop!");
1038 case tok::star: Opc = BinaryOperator::Mul; break;
1039 case tok::slash: Opc = BinaryOperator::Div; break;
1040 case tok::percent: Opc = BinaryOperator::Rem; break;
1041 case tok::plus: Opc = BinaryOperator::Add; break;
1042 case tok::minus: Opc = BinaryOperator::Sub; break;
1043 case tok::lessless: Opc = BinaryOperator::Shl; break;
1044 case tok::greatergreater: Opc = BinaryOperator::Shr; break;
1045 case tok::lessequal: Opc = BinaryOperator::LE; break;
1046 case tok::less: Opc = BinaryOperator::LT; break;
1047 case tok::greaterequal: Opc = BinaryOperator::GE; break;
1048 case tok::greater: Opc = BinaryOperator::GT; break;
1049 case tok::exclaimequal: Opc = BinaryOperator::NE; break;
1050 case tok::equalequal: Opc = BinaryOperator::EQ; break;
1051 case tok::amp: Opc = BinaryOperator::And; break;
1052 case tok::caret: Opc = BinaryOperator::Xor; break;
1053 case tok::pipe: Opc = BinaryOperator::Or; break;
1054 case tok::ampamp: Opc = BinaryOperator::LAnd; break;
1055 case tok::pipepipe: Opc = BinaryOperator::LOr; break;
1056 case tok::equal: Opc = BinaryOperator::Assign; break;
1057 case tok::starequal: Opc = BinaryOperator::MulAssign; break;
1058 case tok::slashequal: Opc = BinaryOperator::DivAssign; break;
1059 case tok::percentequal: Opc = BinaryOperator::RemAssign; break;
1060 case tok::plusequal: Opc = BinaryOperator::AddAssign; break;
1061 case tok::minusequal: Opc = BinaryOperator::SubAssign; break;
1062 case tok::lesslessequal: Opc = BinaryOperator::ShlAssign; break;
1063 case tok::greatergreaterequal: Opc = BinaryOperator::ShrAssign; break;
1064 case tok::ampequal: Opc = BinaryOperator::AndAssign; break;
1065 case tok::caretequal: Opc = BinaryOperator::XorAssign; break;
1066 case tok::pipeequal: Opc = BinaryOperator::OrAssign; break;
1067 case tok::comma: Opc = BinaryOperator::Comma; break;
1068 }
1069 return Opc;
1070}
1071
Steve Naroff35d85152007-05-07 00:24:15 +00001072static inline UnaryOperator::Opcode ConvertTokenKindToUnaryOpcode(
1073 tok::TokenKind Kind) {
1074 UnaryOperator::Opcode Opc;
1075 switch (Kind) {
1076 default: assert(0 && "Unknown unary op!");
1077 case tok::plusplus: Opc = UnaryOperator::PreInc; break;
1078 case tok::minusminus: Opc = UnaryOperator::PreDec; break;
1079 case tok::amp: Opc = UnaryOperator::AddrOf; break;
1080 case tok::star: Opc = UnaryOperator::Deref; break;
1081 case tok::plus: Opc = UnaryOperator::Plus; break;
1082 case tok::minus: Opc = UnaryOperator::Minus; break;
1083 case tok::tilde: Opc = UnaryOperator::Not; break;
1084 case tok::exclaim: Opc = UnaryOperator::LNot; break;
1085 case tok::kw_sizeof: Opc = UnaryOperator::SizeOf; break;
1086 case tok::kw___alignof: Opc = UnaryOperator::AlignOf; break;
1087 case tok::kw___real: Opc = UnaryOperator::Real; break;
1088 case tok::kw___imag: Opc = UnaryOperator::Imag; break;
Steve Naroff35d85152007-05-07 00:24:15 +00001089 // FIXME: case tok::kw___extension__:
1090 }
1091 return Opc;
1092}
1093
Steve Naroff218bc2b2007-05-04 21:54:46 +00001094// Binary Operators. 'Tok' is the token for the operator.
1095Action::ExprResult Sema::ParseBinOp(SourceLocation TokLoc, tok::TokenKind Kind,
1096 ExprTy *LHS, ExprTy *RHS) {
1097 BinaryOperator::Opcode Opc = ConvertTokenKindToBinaryOpcode(Kind);
1098 Expr *lhs = (Expr *)LHS, *rhs = (Expr*)RHS;
1099
1100 assert((lhs != 0) && "ParseBinOp(): missing left expression");
1101 assert((rhs != 0) && "ParseBinOp(): missing right expression");
1102
1103 QualType result;
1104
1105 switch (Opc) {
1106 default:
1107 assert(0 && "Unknown binary expr!");
1108 case BinaryOperator::Assign:
Steve Naroff35d85152007-05-07 00:24:15 +00001109 result = CheckAssignmentOperands(lhs, rhs, TokLoc, QualType());
Steve Naroff218bc2b2007-05-04 21:54:46 +00001110 break;
1111 case BinaryOperator::Mul:
1112 case BinaryOperator::Div:
1113 result = CheckMultiplyDivideOperands(lhs, rhs, TokLoc);
1114 break;
1115 case BinaryOperator::Rem:
1116 result = CheckRemainderOperands(lhs, rhs, TokLoc);
1117 break;
1118 case BinaryOperator::Add:
1119 result = CheckAdditionOperands(lhs, rhs, TokLoc);
1120 break;
1121 case BinaryOperator::Sub:
1122 result = CheckSubtractionOperands(lhs, rhs, TokLoc);
1123 break;
1124 case BinaryOperator::Shl:
1125 case BinaryOperator::Shr:
1126 result = CheckShiftOperands(lhs, rhs, TokLoc);
1127 break;
1128 case BinaryOperator::LE:
1129 case BinaryOperator::LT:
1130 case BinaryOperator::GE:
1131 case BinaryOperator::GT:
1132 result = CheckRelationalOperands(lhs, rhs, TokLoc);
1133 break;
1134 case BinaryOperator::EQ:
1135 case BinaryOperator::NE:
1136 result = CheckEqualityOperands(lhs, rhs, TokLoc);
1137 break;
1138 case BinaryOperator::And:
1139 case BinaryOperator::Xor:
1140 case BinaryOperator::Or:
1141 result = CheckBitwiseOperands(lhs, rhs, TokLoc);
1142 break;
1143 case BinaryOperator::LAnd:
1144 case BinaryOperator::LOr:
1145 result = CheckLogicalOperands(lhs, rhs, TokLoc);
1146 break;
1147 case BinaryOperator::MulAssign:
1148 case BinaryOperator::DivAssign:
1149 result = CheckMultiplyDivideOperands(lhs, rhs, TokLoc);
1150 if (result.isNull())
1151 return true;
Steve Naroff35d85152007-05-07 00:24:15 +00001152 result = CheckAssignmentOperands(lhs, rhs, TokLoc, result);
Steve Naroff218bc2b2007-05-04 21:54:46 +00001153 break;
1154 case BinaryOperator::RemAssign:
1155 result = CheckRemainderOperands(lhs, rhs, TokLoc);
1156 if (result.isNull())
1157 return true;
Steve Naroff35d85152007-05-07 00:24:15 +00001158 result = CheckAssignmentOperands(lhs, rhs, TokLoc, result);
Steve Naroff218bc2b2007-05-04 21:54:46 +00001159 break;
1160 case BinaryOperator::AddAssign:
1161 result = CheckAdditionOperands(lhs, rhs, TokLoc);
1162 if (result.isNull())
1163 return true;
Steve Naroff35d85152007-05-07 00:24:15 +00001164 result = CheckAssignmentOperands(lhs, rhs, TokLoc, result);
Steve Naroff218bc2b2007-05-04 21:54:46 +00001165 break;
1166 case BinaryOperator::SubAssign:
1167 result = CheckSubtractionOperands(lhs, rhs, TokLoc);
1168 if (result.isNull())
1169 return true;
Steve Naroff35d85152007-05-07 00:24:15 +00001170 result = CheckAssignmentOperands(lhs, rhs, TokLoc, result);
Steve Naroff218bc2b2007-05-04 21:54:46 +00001171 break;
1172 case BinaryOperator::ShlAssign:
1173 case BinaryOperator::ShrAssign:
1174 result = CheckShiftOperands(lhs, rhs, TokLoc);
1175 if (result.isNull())
1176 return true;
Steve Naroff35d85152007-05-07 00:24:15 +00001177 result = CheckAssignmentOperands(lhs, rhs, TokLoc, result);
Steve Naroff218bc2b2007-05-04 21:54:46 +00001178 break;
1179 case BinaryOperator::AndAssign:
1180 case BinaryOperator::XorAssign:
1181 case BinaryOperator::OrAssign:
1182 result = CheckBitwiseOperands(lhs, rhs, TokLoc);
1183 if (result.isNull())
1184 return true;
Steve Naroff35d85152007-05-07 00:24:15 +00001185 result = CheckAssignmentOperands(lhs, rhs, TokLoc, result);
Steve Naroff218bc2b2007-05-04 21:54:46 +00001186 break;
1187 case BinaryOperator::Comma:
1188 result = CheckCommaOperands(lhs, rhs, TokLoc);
1189 break;
1190 }
1191 if (result.isNull())
1192 return true;
1193 return new BinaryOperator(lhs, rhs, Opc, result);
1194}
1195
Steve Naroff35d85152007-05-07 00:24:15 +00001196// Unary Operators. 'Tok' is the token for the operator.
1197Action::ExprResult Sema::ParseUnaryOp(SourceLocation OpLoc, tok::TokenKind Op,
1198 ExprTy *Input) {
1199 UnaryOperator::Opcode Opc = ConvertTokenKindToUnaryOpcode(Op);
1200 QualType resultType;
1201 switch (Opc) {
1202 default:
1203 assert(0 && "Unimplemented unary expr!");
1204 case UnaryOperator::PreInc:
1205 case UnaryOperator::PreDec:
Steve Naroff71ce2e02007-05-18 22:53:50 +00001206 resultType = CheckIncrementDecrementOperand((Expr *)Input, OpLoc);
Steve Naroff35d85152007-05-07 00:24:15 +00001207 break;
1208 case UnaryOperator::AddrOf:
1209 resultType = CheckAddressOfOperand((Expr *)Input, OpLoc);
1210 break;
1211 case UnaryOperator::Deref:
1212 resultType = CheckIndirectionOperand((Expr *)Input, OpLoc);
1213 break;
1214 case UnaryOperator::Plus:
1215 case UnaryOperator::Minus:
1216 resultType = UsualUnaryConversion(((Expr *)Input)->getType());
1217 if (!resultType->isArithmeticType()) // C99 6.5.3.3p1
Chris Lattnerc04bd6a2007-05-16 18:09:54 +00001218 return Diag(OpLoc, diag::err_typecheck_unary_expr,
1219 resultType.getAsString());
Steve Naroff35d85152007-05-07 00:24:15 +00001220 break;
1221 case UnaryOperator::Not: // bitwise complement
1222 resultType = UsualUnaryConversion(((Expr *)Input)->getType());
1223 if (!resultType->isIntegerType()) // C99 6.5.3.3p1
Chris Lattnerc04bd6a2007-05-16 18:09:54 +00001224 return Diag(OpLoc, diag::err_typecheck_unary_expr,
1225 resultType.getAsString());
Steve Naroff35d85152007-05-07 00:24:15 +00001226 break;
1227 case UnaryOperator::LNot: // logical negation
1228 resultType = UsualUnaryConversion(((Expr *)Input)->getType());
1229 if (!resultType->isScalarType()) // C99 6.5.3.3p1
Chris Lattnerc04bd6a2007-05-16 18:09:54 +00001230 return Diag(OpLoc, diag::err_typecheck_unary_expr,
1231 resultType.getAsString());
Steve Naroff35d85152007-05-07 00:24:15 +00001232 break;
1233 case UnaryOperator::SizeOf:
Steve Naroff043d45d2007-05-15 02:32:35 +00001234 resultType = CheckSizeOfAlignOfOperand(((Expr *)Input)->getType(), OpLoc,
1235 true);
1236 break;
Steve Naroff35d85152007-05-07 00:24:15 +00001237 case UnaryOperator::AlignOf:
Steve Naroff043d45d2007-05-15 02:32:35 +00001238 resultType = CheckSizeOfAlignOfOperand(((Expr *)Input)->getType(), OpLoc,
1239 false);
1240 break;
Steve Naroff35d85152007-05-07 00:24:15 +00001241 }
1242 if (resultType.isNull())
1243 return true;
Steve Naroff509fe022007-05-17 01:16:00 +00001244 return new UnaryOperator((Expr *)Input, Opc, resultType, OpLoc);
Steve Naroff35d85152007-05-07 00:24:15 +00001245}
Chris Lattnereefa10e2007-05-28 06:56:27 +00001246
1247/// ParseAddrLabel - Parse the GNU address of label extension: "&&foo".
1248Sema::ExprResult Sema::ParseAddrLabel(SourceLocation OpLoc,
1249 SourceLocation LabLoc,
1250 IdentifierInfo *LabelII) {
1251 // Look up the record for this label identifier.
1252 LabelStmt *&LabelDecl = LabelMap[LabelII];
1253
1254 // If we haven't seen this label yet, create a forward reference.
1255 if (LabelDecl == 0)
1256 LabelDecl = new LabelStmt(LabLoc, LabelII, 0);
1257
1258 // Create the AST node. The address of a label always has type 'void*'.
1259 return new AddrLabel(OpLoc, LabLoc, LabelDecl,
1260 Context.getPointerType(Context.VoidTy));
1261}
1262