blob: a148f22cbdfb0eb869ca30a852b7278429c42c0f [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 // Pass &StringTokLocs[0], StringTokLocs.size() to factory!
Steve Naroff4f88b312007-03-13 22:37:02 +000050 return new StringLiteral(Literal.GetString(), Literal.GetStringLength(),
Steve Naroff53f07dc2007-05-17 21:49:33 +000051 Literal.AnyWide, t, StringToks[0].getLocation(),
52 StringToks[NumStringToks-1].getLocation());
Chris Lattner5b183d82006-11-10 05:03:26 +000053}
54
Chris Lattnere168f762006-11-10 05:29:30 +000055
Chris Lattnerac18be92006-11-20 06:49:47 +000056/// ParseIdentifierExpr - The parser read an identifier in expression context,
57/// validate it per-C99 6.5.1. HasTrailingLParen indicates whether this
58/// identifier is used in an function call context.
59Sema::ExprResult Sema::ParseIdentifierExpr(Scope *S, SourceLocation Loc,
60 IdentifierInfo &II,
61 bool HasTrailingLParen) {
Chris Lattner17ed4872006-11-20 04:58:19 +000062 // Could be enum-constant or decl.
Chris Lattner9561a0b2007-01-28 08:20:04 +000063 Decl *D = LookupScopedDecl(&II, Decl::IDNS_Ordinary, Loc, S);
Chris Lattner17ed4872006-11-20 04:58:19 +000064 if (D == 0) {
Bill Wendling4073ed52007-02-13 01:51:42 +000065 // Otherwise, this could be an implicitly declared function reference (legal
Chris Lattner9561a0b2007-01-28 08:20:04 +000066 // in C90, extension in C99).
Chris Lattnerac18be92006-11-20 06:49:47 +000067 if (HasTrailingLParen &&
68 // Not in C++.
Steve Narofff1e53692007-03-23 22:27:02 +000069 !getLangOptions().CPlusPlus)
Chris Lattnerac18be92006-11-20 06:49:47 +000070 D = ImplicitlyDefineFunction(Loc, II, S);
Steve Naroff92e30f82007-04-02 22:35:25 +000071 else {
Chris Lattnerac18be92006-11-20 06:49:47 +000072 // If this name wasn't predeclared and if this is not a function call,
73 // diagnose the problem.
Steve Narofff1e53692007-03-23 22:27:02 +000074 return Diag(Loc, diag::err_undeclared_var_use, II.getName());
Steve Naroff92e30f82007-04-02 22:35:25 +000075 }
Chris Lattner17ed4872006-11-20 04:58:19 +000076 }
77
Steve Naroff46ba1eb2007-04-03 23:13:13 +000078 if (ValueDecl *VD = dyn_cast<ValueDecl>(D))
Steve Naroff509fe022007-05-17 01:16:00 +000079 return new DeclRefExpr(VD, VD->getType(), Loc);
Steve Naroff46ba1eb2007-04-03 23:13:13 +000080 if (isa<TypedefDecl>(D))
Steve Narofff1e53692007-03-23 22:27:02 +000081 return Diag(Loc, diag::err_unexpected_typedef, II.getName());
82
83 assert(0 && "Invalid decl");
Chris Lattner17ed4872006-11-20 04:58:19 +000084}
Chris Lattnere168f762006-11-10 05:29:30 +000085
Chris Lattner17ed4872006-11-20 04:58:19 +000086Sema::ExprResult Sema::ParseSimplePrimaryExpr(SourceLocation Loc,
87 tok::TokenKind Kind) {
Chris Lattnere168f762006-11-10 05:29:30 +000088 switch (Kind) {
89 default:
90 assert(0 && "Unknown simple primary expr!");
Steve Naroffe4718892007-04-27 18:30:00 +000091 // TODO: MOVE this to be some other callback.
Chris Lattnere168f762006-11-10 05:29:30 +000092 case tok::kw___func__: // primary-expression: __func__ [C99 6.4.2.2]
93 case tok::kw___FUNCTION__: // primary-expression: __FUNCTION__ [GNU]
94 case tok::kw___PRETTY_FUNCTION__: // primary-expression: __P..Y_F..N__ [GNU]
Chris Lattner17ed4872006-11-20 04:58:19 +000095 return 0;
Chris Lattnere168f762006-11-10 05:29:30 +000096 }
97}
98
Steve Naroffae4143e2007-04-26 20:39:23 +000099Sema::ExprResult Sema::ParseCharacterConstant(const LexerToken &Tok) {
100 SmallString<16> CharBuffer;
101 CharBuffer.resize(Tok.getLength());
102 const char *ThisTokBegin = &CharBuffer[0];
103 unsigned ActualLength = PP.getSpelling(Tok, ThisTokBegin);
104
105 CharLiteralParser Literal(ThisTokBegin, ThisTokBegin+ActualLength,
106 Tok.getLocation(), PP);
107 if (Literal.hadError())
108 return ExprResult(true);
Steve Naroff509fe022007-05-17 01:16:00 +0000109 return new CharacterLiteral(Literal.getValue(), Context.IntTy,
110 Tok.getLocation());
Steve Naroffae4143e2007-04-26 20:39:23 +0000111}
112
Steve Naroff8160ea22007-03-06 01:09:46 +0000113Action::ExprResult Sema::ParseNumericConstant(const LexerToken &Tok) {
Steve Narofff2fb89e2007-03-13 20:29:44 +0000114 // fast path for a single digit (which is quite common). A single digit
115 // cannot have a trigraph, escaped newline, radix prefix, or type suffix.
116 if (Tok.getLength() == 1) {
117 const char *t = PP.getSourceManager().getCharacterData(Tok.getLocation());
Chris Lattner67ca9252007-05-21 01:08:44 +0000118
119 unsigned IntSize = Context.Target.getIntWidth(Tok.getLocation());
120 return ExprResult(new IntegerLiteral(APInt(IntSize, *t-'0'), Context.IntTy,
Steve Naroff509fe022007-05-17 01:16:00 +0000121 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
Chris Lattner67ca9252007-05-21 01:08:44 +0000127 // Get the spelling of the token, which eliminates trigraphs, etc.
Steve Naroff8160ea22007-03-06 01:09:46 +0000128 unsigned ActualLength = PP.getSpelling(Tok, ThisTokBegin);
Steve Naroff09ef4742007-03-09 23:16:33 +0000129 NumericLiteralParser Literal(ThisTokBegin, ThisTokBegin+ActualLength,
Steve Naroff451d8f162007-03-12 23:22:38 +0000130 Tok.getLocation(), PP);
Steve Narofff2fb89e2007-03-13 20:29:44 +0000131 if (Literal.hadError)
132 return ExprResult(true);
Chris Lattner67ca9252007-05-21 01:08:44 +0000133
Steve Naroff09ef4742007-03-09 23:16:33 +0000134 if (Literal.isIntegerLiteral()) {
Steve Naroffe5aa9be2007-04-05 22:36:20 +0000135 QualType t;
Chris Lattner67ca9252007-05-21 01:08:44 +0000136
137 // Get the value in the widest-possible width.
138 APInt ResultVal(Context.Target.getIntMaxTWidth(Tok.getLocation()), 0);
139
140 if (Literal.GetIntegerValue(ResultVal)) {
141 // If this value didn't fit into uintmax_t, warn and force to ull.
142 Diag(Tok.getLocation(), diag::warn_integer_too_large);
143 t = Context.UnsignedLongLongTy;
144 assert(Context.getIntegerBitwidth(t, Tok.getLocation()) ==
145 ResultVal.getBitWidth() && "long long is not intmax_t?");
Steve Naroff09ef4742007-03-09 23:16:33 +0000146 } else {
Chris Lattner67ca9252007-05-21 01:08:44 +0000147 // If this value fits into a ULL, try to figure out what else it fits into
148 // according to the rules of C99 6.4.4.1p5.
149
150 // Octal, Hexadecimal, and integers with a U suffix are allowed to
151 // be an unsigned int.
152 bool AllowUnsigned = Literal.isUnsigned || Literal.getRadix() != 10;
153
154 // Check from smallest to largest, picking the smallest type we can.
155 if (!Literal.isLong) { // Are int/unsigned possibilities?
156 unsigned IntSize = Context.Target.getIntWidth(Tok.getLocation());
157 // Does it fit in a unsigned int?
158 if (ResultVal.isIntN(IntSize)) {
159 // Does it fit in a signed int?
160 if (!Literal.isUnsigned && ResultVal[IntSize-1] == 0)
161 t = Context.IntTy;
162 else if (AllowUnsigned)
163 t = Context.UnsignedIntTy;
164 }
165
166 if (!t.isNull())
167 ResultVal.trunc(IntSize);
168 }
169
170 // Are long/unsigned long possibilities?
171 if (t.isNull() && !Literal.isLongLong) {
172 unsigned LongSize = Context.Target.getLongWidth(Tok.getLocation());
173
174 // Does it fit in a unsigned long?
175 if (ResultVal.isIntN(LongSize)) {
176 // Does it fit in a signed long?
177 if (!Literal.isUnsigned && ResultVal[LongSize-1] == 0)
178 t = Context.LongTy;
179 else if (AllowUnsigned)
180 t = Context.UnsignedLongTy;
181 }
182 if (!t.isNull())
183 ResultVal.trunc(LongSize);
184 }
185
186 // Finally, check long long if needed.
187 if (t.isNull()) {
188 unsigned LongLongSize =
189 Context.Target.getLongLongWidth(Tok.getLocation());
190
191 // Does it fit in a unsigned long long?
192 if (ResultVal.isIntN(LongLongSize)) {
193 // Does it fit in a signed long long?
194 if (!Literal.isUnsigned && ResultVal[LongLongSize-1] == 0)
195 t = Context.LongLongTy;
196 else if (AllowUnsigned)
197 t = Context.UnsignedLongLongTy;
198 }
199 }
200
201 // If we still couldn't decide a type, we probably have something that
202 // does not fit in a signed long long, but has no U suffix.
203 if (t.isNull()) {
204 Diag(Tok.getLocation(), diag::warn_integer_too_large_for_signed);
205 t = Context.UnsignedLongLongTy;
206 }
Steve Naroff09ef4742007-03-09 23:16:33 +0000207 }
Chris Lattner67ca9252007-05-21 01:08:44 +0000208
209 return new IntegerLiteral(ResultVal, t, Tok.getLocation());
Steve Naroff09ef4742007-03-09 23:16:33 +0000210 } else if (Literal.isFloatingLiteral()) {
Steve Narofff1e53692007-03-23 22:27:02 +0000211 // FIXME: fill in the value and compute the real type...
Steve Naroff509fe022007-05-17 01:16:00 +0000212 return new FloatingLiteral(7.7, Context.FloatTy, Tok.getLocation());
Steve Naroff09ef4742007-03-09 23:16:33 +0000213 }
Steve Narofff2fb89e2007-03-13 20:29:44 +0000214 return ExprResult(true);
Chris Lattnere168f762006-11-10 05:29:30 +0000215}
216
217Action::ExprResult Sema::ParseParenExpr(SourceLocation L, SourceLocation R,
218 ExprTy *Val) {
Steve Naroffae4143e2007-04-26 20:39:23 +0000219 Expr *e = (Expr *)Val;
220 assert((e != 0) && "ParseParenExpr() missing expr");
Steve Naroff53f07dc2007-05-17 21:49:33 +0000221 return new ParenExpr(L, R, e);
Chris Lattnere168f762006-11-10 05:29:30 +0000222}
223
Steve Naroff71b59a92007-06-04 22:22:31 +0000224/// The UsualUnaryConversions() function is *not* called by this routine.
Steve Naroffa78fe7e2007-05-16 19:47:19 +0000225/// See C99 6.3.2.1p[2-4] for more details.
Steve Naroff043d45d2007-05-15 02:32:35 +0000226QualType Sema::CheckSizeOfAlignOfOperand(QualType exprType,
227 SourceLocation OpLoc, bool isSizeof) {
228 // C99 6.5.3.4p1:
229 if (isa<FunctionType>(exprType) && isSizeof)
230 // alignof(function) is allowed.
231 Diag(OpLoc, diag::ext_sizeof_function_type);
232 else if (exprType->isVoidType())
233 Diag(OpLoc, diag::ext_sizeof_void_type, isSizeof ? "sizeof" : "__alignof");
234 else if (exprType->isIncompleteType()) {
Steve Naroff043d45d2007-05-15 02:32:35 +0000235 Diag(OpLoc, isSizeof ? diag::err_sizeof_incomplete_type :
Chris Lattner3dc3d772007-05-16 18:07:12 +0000236 diag::err_alignof_incomplete_type,
237 exprType.getAsString());
Steve Naroff043d45d2007-05-15 02:32:35 +0000238 return QualType(); // error
239 }
240 // C99 6.5.3.4p4: the type (an unsigned integer type) is size_t.
241 return Context.getSizeType();
242}
243
Chris Lattnere168f762006-11-10 05:29:30 +0000244Action::ExprResult Sema::
245ParseSizeOfAlignOfTypeExpr(SourceLocation OpLoc, bool isSizeof,
Steve Naroff509fe022007-05-17 01:16:00 +0000246 SourceLocation LPLoc, TypeTy *Ty,
247 SourceLocation RPLoc) {
Chris Lattner0d8b1a12006-11-20 04:34:45 +0000248 // If error parsing type, ignore.
249 if (Ty == 0) return true;
Chris Lattner6531c102007-01-23 22:29:49 +0000250
251 // Verify that this is a valid expression.
Steve Naroffe5aa9be2007-04-05 22:36:20 +0000252 QualType ArgTy = QualType::getFromOpaquePtr(Ty);
Chris Lattner6531c102007-01-23 22:29:49 +0000253
Steve Naroff043d45d2007-05-15 02:32:35 +0000254 QualType resultType = CheckSizeOfAlignOfOperand(ArgTy, OpLoc, isSizeof);
255
256 if (resultType.isNull())
257 return true;
Steve Naroff509fe022007-05-17 01:16:00 +0000258 return new SizeOfAlignOfTypeExpr(isSizeof, ArgTy, resultType, OpLoc, RPLoc);
Chris Lattnere168f762006-11-10 05:29:30 +0000259}
260
261
262Action::ExprResult Sema::ParsePostfixUnaryOp(SourceLocation OpLoc,
263 tok::TokenKind Kind,
264 ExprTy *Input) {
265 UnaryOperator::Opcode Opc;
266 switch (Kind) {
267 default: assert(0 && "Unknown unary op!");
268 case tok::plusplus: Opc = UnaryOperator::PostInc; break;
269 case tok::minusminus: Opc = UnaryOperator::PostDec; break;
270 }
Steve Naroff71ce2e02007-05-18 22:53:50 +0000271 QualType result = CheckIncrementDecrementOperand((Expr *)Input, OpLoc);
Steve Naroff35d85152007-05-07 00:24:15 +0000272 if (result.isNull())
273 return true;
Steve Naroff509fe022007-05-17 01:16:00 +0000274 return new UnaryOperator((Expr *)Input, Opc, result, OpLoc);
Chris Lattnere168f762006-11-10 05:29:30 +0000275}
276
277Action::ExprResult Sema::
278ParseArraySubscriptExpr(ExprTy *Base, SourceLocation LLoc,
279 ExprTy *Idx, SourceLocation RLoc) {
Steve Naroffe5aa9be2007-04-05 22:36:20 +0000280 QualType t1 = ((Expr *)Base)->getType();
281 QualType t2 = ((Expr *)Idx)->getType();
Steve Narofff1e53692007-03-23 22:27:02 +0000282
283 assert(!t1.isNull() && "no type for array base expression");
Steve Naroffc1aadb12007-03-28 21:49:40 +0000284 assert(!t2.isNull() && "no type for array index expression");
Steve Narofff1e53692007-03-23 22:27:02 +0000285
Steve Naroffe5aa9be2007-04-05 22:36:20 +0000286 QualType canonT1 = t1.getCanonicalType();
287 QualType canonT2 = t2.getCanonicalType();
Steve Naroffd50c88e2007-04-05 21:15:20 +0000288
Steve Naroffc1aadb12007-03-28 21:49:40 +0000289 // C99 6.5.2.1p2: the expression e1[e2] is by definition precisely equivalent
290 // to the expression *((e1)+(e2)). This means the array "Base" may actually be
Steve Narofff1e53692007-03-23 22:27:02 +0000291 // in the subscript position. As a result, we need to derive the array base
292 // and index from the expression types.
293
Steve Naroffe5aa9be2007-04-05 22:36:20 +0000294 QualType baseType, indexType;
Steve Naroffd50c88e2007-04-05 21:15:20 +0000295 if (isa<ArrayType>(canonT1) || isa<PointerType>(canonT1)) {
296 baseType = canonT1;
297 indexType = canonT2;
298 } else if (isa<ArrayType>(canonT2) || isa<PointerType>(canonT2)) { // uncommon
299 baseType = canonT2;
300 indexType = canonT1;
Steve Narofff1e53692007-03-23 22:27:02 +0000301 } else
302 return Diag(LLoc, diag::err_typecheck_subscript_value);
303
Steve Naroffc1aadb12007-03-28 21:49:40 +0000304 // C99 6.5.2.1p1
Steve Naroff1926c832007-04-24 00:23:05 +0000305 if (!indexType->isIntegerType())
Steve Narofff1e53692007-03-23 22:27:02 +0000306 return Diag(LLoc, diag::err_typecheck_subscript);
Steve Naroffc1aadb12007-03-28 21:49:40 +0000307
Steve Naroff95af0132007-03-30 23:47:58 +0000308 // FIXME: need to deal with const...
Steve Naroffe5aa9be2007-04-05 22:36:20 +0000309 QualType resultType;
Steve Naroffc1aadb12007-03-28 21:49:40 +0000310 if (ArrayType *ary = dyn_cast<ArrayType>(baseType)) {
311 resultType = ary->getElementType();
312 } else if (PointerType *ary = dyn_cast<PointerType>(baseType)) {
313 resultType = ary->getPointeeType();
314 // in practice, the following check catches trying to index a pointer
315 // to a function (e.g. void (*)(int)). Functions are not objects in c99.
Steve Naroffbc2f0992007-03-30 20:09:34 +0000316 if (!resultType->isObjectType())
Chris Lattnerc04bd6a2007-05-16 18:09:54 +0000317 return Diag(LLoc, diag::err_typecheck_subscript_not_object,
318 baseType.getAsString());
Steve Naroffc1aadb12007-03-28 21:49:40 +0000319 }
Steve Naroff509fe022007-05-17 01:16:00 +0000320 return new ArraySubscriptExpr((Expr*)Base, (Expr*)Idx, resultType, RLoc);
Chris Lattnere168f762006-11-10 05:29:30 +0000321}
322
323Action::ExprResult Sema::
324ParseMemberReferenceExpr(ExprTy *Base, SourceLocation OpLoc,
325 tok::TokenKind OpKind, SourceLocation MemberLoc,
326 IdentifierInfo &Member) {
Steve Naroffe5aa9be2007-04-05 22:36:20 +0000327 QualType qualifiedType = ((Expr *)Base)->getType();
Steve Naroffca8f7122007-04-01 01:41:35 +0000328
329 assert(!qualifiedType.isNull() && "no type for member expression");
330
Steve Naroffe5aa9be2007-04-05 22:36:20 +0000331 QualType canonType = qualifiedType.getCanonicalType();
Steve Narofff1e53692007-03-23 22:27:02 +0000332
333 if (OpKind == tok::arrow) {
Steve Naroffca8f7122007-04-01 01:41:35 +0000334 if (PointerType *PT = dyn_cast<PointerType>(canonType)) {
335 qualifiedType = PT->getPointeeType();
Steve Naroffd50c88e2007-04-05 21:15:20 +0000336 canonType = qualifiedType.getCanonicalType();
Steve Naroffca8f7122007-04-01 01:41:35 +0000337 } else
Steve Narofff1e53692007-03-23 22:27:02 +0000338 return Diag(OpLoc, diag::err_typecheck_member_reference_arrow);
339 }
Steve Naroff92e30f82007-04-02 22:35:25 +0000340 if (!isa<RecordType>(canonType))
341 return Diag(OpLoc, diag::err_typecheck_member_reference_structUnion);
342
343 // get the struct/union definition from the type.
344 RecordDecl *RD = cast<RecordType>(canonType)->getDecl();
Steve Naroffcc321422007-03-26 23:09:51 +0000345
Steve Naroff92e30f82007-04-02 22:35:25 +0000346 if (canonType->isIncompleteType())
347 return Diag(OpLoc, diag::err_typecheck_incomplete_tag, RD->getName());
Steve Naroffcc321422007-03-26 23:09:51 +0000348
Steve Naroff92e30f82007-04-02 22:35:25 +0000349 FieldDecl *MemberDecl = RD->getMember(&Member);
350 if (!MemberDecl)
351 return Diag(OpLoc, diag::err_typecheck_no_member, Member.getName());
352
Steve Naroff9358c712007-05-27 23:58:33 +0000353 return new MemberExpr((Expr*)Base, OpKind == tok::arrow,
354 MemberDecl, MemberLoc);
Chris Lattnere168f762006-11-10 05:29:30 +0000355}
356
357/// ParseCallExpr - Handle a call to Fn with the specified array of arguments.
358/// This provides the location of the left/right parens and a list of comma
359/// locations.
360Action::ExprResult Sema::
361ParseCallExpr(ExprTy *Fn, SourceLocation LParenLoc,
Steve Naroffb8c289d2007-05-08 22:18:00 +0000362 ExprTy **Args, unsigned NumArgsInCall,
Chris Lattnere168f762006-11-10 05:29:30 +0000363 SourceLocation *CommaLocs, SourceLocation RParenLoc) {
Steve Naroff8563f652007-05-28 19:25:56 +0000364 Expr *funcExpr = (Expr *)Fn;
Steve Naroff8563f652007-05-28 19:25:56 +0000365 assert(funcExpr && "no function call expression");
366
Chris Lattner3343f812007-06-06 05:05:41 +0000367 QualType qType = UsualUnaryConversions(funcExpr->getType());
Steve Naroffae4143e2007-04-26 20:39:23 +0000368 assert(!qType.isNull() && "no type for function call expression");
369
Chris Lattner3d01e4e2007-06-06 05:14:05 +0000370 // C99 6.5.2.2p1 - "The expression that denotes the called function shall have
371 // type pointer to function".
372 const PointerType *PT = dyn_cast<PointerType>(qType);
373 if (PT == 0) PT = dyn_cast<PointerType>(qType.getCanonicalType());
374
375 if (PT == 0)
376 return Diag(funcExpr->getLocStart(), diag::err_typecheck_call_not_function,
377 SourceRange(funcExpr->getLocStart(), RParenLoc));
Chris Lattner3343f812007-06-06 05:05:41 +0000378
379 const FunctionType *funcT = dyn_cast<FunctionType>(PT->getPointeeType());
Chris Lattner3d01e4e2007-06-06 05:14:05 +0000380 if (funcT == 0)
381 funcT = dyn_cast<FunctionType>(PT->getPointeeType().getCanonicalType());
382
383 if (funcT == 0)
384 return Diag(funcExpr->getLocStart(), diag::err_typecheck_call_not_function,
385 SourceRange(funcExpr->getLocStart(), RParenLoc));
Steve Naroffb8c289d2007-05-08 22:18:00 +0000386
Steve Naroff17f76e02007-05-03 21:03:48 +0000387 // If a prototype isn't declared, the parser implicitly defines a func decl
388 QualType resultType = funcT->getResultType();
389
390 if (const FunctionTypeProto *proto = dyn_cast<FunctionTypeProto>(funcT)) {
391 // C99 6.5.2.2p7 - the arguments are implicitly converted, as if by
392 // assignment, to the types of the corresponding parameter, ...
393
394 unsigned NumArgsInProto = proto->getNumArgs();
Steve Naroffb8c289d2007-05-08 22:18:00 +0000395 unsigned NumArgsToCheck = NumArgsInCall;
Steve Naroff17f76e02007-05-03 21:03:48 +0000396
Steve Naroffb8c289d2007-05-08 22:18:00 +0000397 if (NumArgsInCall < NumArgsInProto)
Steve Naroff56faab22007-05-30 04:20:12 +0000398 Diag(RParenLoc, diag::err_typecheck_call_too_few_args,
Steve Naroffeb9da942007-05-30 00:06:37 +0000399 funcExpr->getSourceRange());
Steve Naroffb8c289d2007-05-08 22:18:00 +0000400 else if (NumArgsInCall > NumArgsInProto) {
Steve Naroff8563f652007-05-28 19:25:56 +0000401 if (!proto->isVariadic()) {
Steve Naroff56faab22007-05-30 04:20:12 +0000402 Diag(((Expr **)Args)[NumArgsInProto+1]->getLocStart(),
403 diag::err_typecheck_call_too_many_args, funcExpr->getSourceRange(),
404 ((Expr **)Args)[NumArgsInProto+1]->getSourceRange());
Steve Naroff8563f652007-05-28 19:25:56 +0000405 }
Steve Naroffb8c289d2007-05-08 22:18:00 +0000406 NumArgsToCheck = NumArgsInProto;
Steve Naroff17f76e02007-05-03 21:03:48 +0000407 }
408 // Continue to check argument types (even if we have too few/many args).
Steve Naroffb8c289d2007-05-08 22:18:00 +0000409 for (unsigned i = 0; i < NumArgsToCheck; i++) {
Steve Naroff8563f652007-05-28 19:25:56 +0000410 Expr *argExpr = ((Expr **)Args)[i];
Steve Naroff8563f652007-05-28 19:25:56 +0000411 assert(argExpr && "ParseCallExpr(): missing argument expression");
412
Steve Naroff17f76e02007-05-03 21:03:48 +0000413 QualType lhsType = proto->getArgType(i);
Steve Naroff8563f652007-05-28 19:25:56 +0000414 QualType rhsType = argExpr->getType();
Steve Naroff17f76e02007-05-03 21:03:48 +0000415
416 if (lhsType == rhsType) // common case, fast path...
417 continue;
Steve Naroff98cf3e92007-06-06 18:38:38 +0000418
419 AssignmentCheckResult result = CheckAssignmentConstraints(lhsType,
420 rhsType);
Steve Naroff6f49f5d2007-05-29 14:23:36 +0000421 SourceLocation l = argExpr->getLocStart();
Steve Naroff17f76e02007-05-03 21:03:48 +0000422
423 // decode the result (notice that AST's are still created for extensions).
Steve Naroff17f76e02007-05-03 21:03:48 +0000424 switch (result) {
425 case Compatible:
426 break;
427 case PointerFromInt:
Steve Naroff218bc2b2007-05-04 21:54:46 +0000428 // check for null pointer constant (C99 6.3.2.3p3)
Steve Naroffeb9da942007-05-30 00:06:37 +0000429 if (!argExpr->isNullPointerConstant()) {
Steve Naroff56faab22007-05-30 04:20:12 +0000430 Diag(l, diag::ext_typecheck_passing_pointer_int,
431 lhsType.getAsString(), rhsType.getAsString(),
Steve Naroffeb9da942007-05-30 00:06:37 +0000432 funcExpr->getSourceRange(), argExpr->getSourceRange());
433 }
Steve Naroff17f76e02007-05-03 21:03:48 +0000434 break;
435 case IntFromPointer:
Steve Naroff56faab22007-05-30 04:20:12 +0000436 Diag(l, diag::ext_typecheck_passing_pointer_int,
437 lhsType.getAsString(), rhsType.getAsString(),
Steve Naroffeb9da942007-05-30 00:06:37 +0000438 funcExpr->getSourceRange(), argExpr->getSourceRange());
Steve Naroff17f76e02007-05-03 21:03:48 +0000439 break;
440 case IncompatiblePointer:
Steve Naroff8563f652007-05-28 19:25:56 +0000441 Diag(l, diag::ext_typecheck_passing_incompatible_pointer,
Steve Naroffeb9da942007-05-30 00:06:37 +0000442 rhsType.getAsString(), lhsType.getAsString(),
443 funcExpr->getSourceRange(), argExpr->getSourceRange());
Steve Naroff17f76e02007-05-03 21:03:48 +0000444 break;
Steve Naroff1f4d7272007-05-11 04:00:31 +0000445 case CompatiblePointerDiscardsQualifiers:
Steve Naroffeb9da942007-05-30 00:06:37 +0000446 Diag(l, diag::ext_typecheck_passing_discards_qualifiers,
447 rhsType.getAsString(), lhsType.getAsString(),
448 funcExpr->getSourceRange(), argExpr->getSourceRange());
Steve Naroff1f4d7272007-05-11 04:00:31 +0000449 break;
Steve Naroff17f76e02007-05-03 21:03:48 +0000450 case Incompatible:
Steve Naroff8563f652007-05-28 19:25:56 +0000451 return Diag(l, diag::err_typecheck_passing_incompatible,
452 rhsType.getAsString(), lhsType.getAsString(),
453 funcExpr->getSourceRange(), argExpr->getSourceRange());
Steve Naroff17f76e02007-05-03 21:03:48 +0000454 }
455 }
Steve Naroffb8c289d2007-05-08 22:18:00 +0000456 // Even if the types checked, bail if we had the wrong number of arguments.
457 if ((NumArgsInCall != NumArgsInProto) && !proto->isVariadic())
458 return true;
Steve Naroffae4143e2007-04-26 20:39:23 +0000459 }
Steve Naroff509fe022007-05-17 01:16:00 +0000460 return new CallExpr((Expr*)Fn, (Expr**)Args, NumArgsInCall, resultType,
461 RParenLoc);
Chris Lattnere168f762006-11-10 05:29:30 +0000462}
463
464Action::ExprResult Sema::
465ParseCastExpr(SourceLocation LParenLoc, TypeTy *Ty,
466 SourceLocation RParenLoc, ExprTy *Op) {
Chris Lattner0d8b1a12006-11-20 04:34:45 +0000467 // If error parsing type, ignore.
Steve Naroffae4143e2007-04-26 20:39:23 +0000468 assert((Ty != 0) && "ParseCastExpr(): missing type");
Chris Lattner1d411a82007-06-05 20:52:21 +0000469 // FIXME: Sema for cast is completely missing.
Steve Naroff509fe022007-05-17 01:16:00 +0000470 return new CastExpr(QualType::getFromOpaquePtr(Ty), (Expr*)Op, LParenLoc);
Chris Lattnere168f762006-11-10 05:29:30 +0000471}
472
Steve Narofff8a28c52007-05-15 20:29:32 +0000473inline QualType Sema::CheckConditionalOperands( // C99 6.5.15
474 Expr *Cond, Expr *LHS, Expr *RHS, SourceLocation questionLoc) {
Steve Naroffa78fe7e2007-05-16 19:47:19 +0000475 QualType cond = Cond->getType();
476 QualType lhs = LHS->getType();
477 QualType rhs = RHS->getType();
478
Steve Narofff8a28c52007-05-15 20:29:32 +0000479 assert(!cond.isNull() && "ParseConditionalOp(): no conditional type");
480 assert(!lhs.isNull() && "ParseConditionalOp(): no lhs type");
481 assert(!rhs.isNull() && "ParseConditionalOp(): no rhs type");
482
Steve Naroff71b59a92007-06-04 22:22:31 +0000483 cond = UsualUnaryConversions(cond);
484 lhs = UsualUnaryConversions(lhs);
485 rhs = UsualUnaryConversions(rhs);
Steve Naroffa78fe7e2007-05-16 19:47:19 +0000486
487 // first, check the condition.
488 if (!cond->isScalarType()) { // C99 6.5.15p2
Steve Naroff53f07dc2007-05-17 21:49:33 +0000489 Diag(Cond->getLocStart(), diag::err_typecheck_cond_expect_scalar,
Steve Naroff509fe022007-05-17 01:16:00 +0000490 cond.getAsString());
Steve Naroffa78fe7e2007-05-16 19:47:19 +0000491 return QualType();
492 }
493 // now check the two expressions.
494 if (lhs->isArithmeticType() && rhs->isArithmeticType()) // C99 6.5.15p3,5
495 return UsualArithmeticConversions(lhs, rhs);
496
497 if ((lhs->isStructureType() && rhs->isStructureType()) || // C99 6.5.15p3
498 (lhs->isUnionType() && rhs->isUnionType())) {
499 TagType *lTag = cast<TagType>(lhs.getCanonicalType());
500 TagType *rTag = cast<TagType>(rhs.getCanonicalType());
501
502 if (lTag->getDecl()->getIdentifier() == rTag->getDecl()->getIdentifier())
503 return lhs;
504 else {
505 Diag(questionLoc, diag::err_typecheck_cond_incompatible_operands,
Steve Naroff3c87a572007-05-28 19:40:22 +0000506 lhs.getAsString(), rhs.getAsString(),
507 LHS->getSourceRange(), RHS->getSourceRange());
Steve Naroffa78fe7e2007-05-16 19:47:19 +0000508 return QualType();
509 }
510 }
Steve Naroff30d1fbc2007-05-20 19:46:53 +0000511 if (lhs->isPointerType() && RHS->isNullPointerConstant()) // C99 6.5.15p3
512 return lhs;
513 if (rhs->isPointerType() && LHS->isNullPointerConstant())
514 return rhs;
515
Steve Naroffa78fe7e2007-05-16 19:47:19 +0000516 if (lhs->isPointerType() && rhs->isPointerType()) { // C99 6.5.15p3,6
517 QualType lhptee, rhptee;
518
519 // get the "pointed to" type
520 lhptee = cast<PointerType>(lhs.getCanonicalType())->getPointeeType();
521 rhptee = cast<PointerType>(rhs.getCanonicalType())->getPointeeType();
522
523 // ignore qualifiers on void (C99 6.5.15p3, clause 6)
524 if (lhptee.getUnqualifiedType()->isVoidType() &&
525 (rhptee->isObjectType() || rhptee->isIncompleteType()))
526 return lhs;
527 if (rhptee.getUnqualifiedType()->isVoidType() &&
528 (lhptee->isObjectType() || lhptee->isIncompleteType()))
529 return rhs;
530
531 // FIXME: C99 6.5.15p6: If both operands are pointers to compatible types
532 // *or* to differently qualified versions of compatible types, the result
533 // type is a pointer to an appropriately qualified version of the
534 // *composite* type.
535 if (!Type::typesAreCompatible(lhptee.getUnqualifiedType(),
536 rhptee.getUnqualifiedType())) {
537 Diag(questionLoc, diag::ext_typecheck_cond_incompatible_pointers,
Steve Naroffeb9da942007-05-30 00:06:37 +0000538 lhs.getAsString(), rhs.getAsString(),
539 LHS->getSourceRange(), RHS->getSourceRange());
Steve Naroffa78fe7e2007-05-16 19:47:19 +0000540 return lhs; // FIXME: this is an _ext - is this return o.k?
541 }
542 }
543 if (lhs->isVoidType() && rhs->isVoidType()) // C99 6.5.15p3
544 return lhs;
Steve Naroffa78fe7e2007-05-16 19:47:19 +0000545
546 Diag(questionLoc, diag::err_typecheck_cond_incompatible_operands,
Steve Naroffeb9da942007-05-30 00:06:37 +0000547 lhs.getAsString(), rhs.getAsString(),
548 LHS->getSourceRange(), RHS->getSourceRange());
Steve Naroffa78fe7e2007-05-16 19:47:19 +0000549 return QualType();
Steve Narofff8a28c52007-05-15 20:29:32 +0000550}
551
Chris Lattnere168f762006-11-10 05:29:30 +0000552/// ParseConditionalOp - Parse a ?: operation. Note that 'LHS' may be null
553/// in the case of a the GNU conditional expr extension.
554Action::ExprResult Sema::ParseConditionalOp(SourceLocation QuestionLoc,
555 SourceLocation ColonLoc,
556 ExprTy *Cond, ExprTy *LHS,
557 ExprTy *RHS) {
Steve Narofff8a28c52007-05-15 20:29:32 +0000558 QualType result = CheckConditionalOperands((Expr *)Cond, (Expr *)LHS,
559 (Expr *)RHS, QuestionLoc);
560 if (result.isNull())
561 return true;
562 return new ConditionalOperator((Expr*)Cond, (Expr*)LHS, (Expr*)RHS, result);
Chris Lattnere168f762006-11-10 05:29:30 +0000563}
564
Steve Naroff71b59a92007-06-04 22:22:31 +0000565inline QualType Sema::DefaultFunctionArrayConversion(QualType t) {
Steve Naroffa78fe7e2007-05-16 19:47:19 +0000566 if (t->isFunctionType()) // C99 6.3.2.1p4
Chris Lattnerea3c20b2007-06-02 22:47:04 +0000567 return Context.getPointerType(t);
Steve Naroffa78fe7e2007-05-16 19:47:19 +0000568 if (const ArrayType *ary = dyn_cast<ArrayType>(t.getCanonicalType()))
569 return Context.getPointerType(ary->getElementType()); // C99 6.3.2.1p3
Steve Naroff1926c832007-04-24 00:23:05 +0000570 return t;
571}
572
Steve Naroff71b59a92007-06-04 22:22:31 +0000573/// UsualUnaryConversion - Performs various conversions that are common to most
574/// operators (C99 6.3). The conversions of array and function types are
575/// sometimes surpressed. For example, the array->pointer conversion doesn't
576/// apply if the array is an argument to the sizeof or address (&) operators.
577/// In these instances, this routine should *not* be called.
578QualType Sema::UsualUnaryConversions(QualType t) {
579 assert(!t.isNull() && "UsualUnaryConversions - missing type");
580
581 if (t->isPromotableIntegerType()) // C99 6.3.1.1p2
582 return Context.IntTy;
583 return DefaultFunctionArrayConversion(t);
584}
585
Steve Naroff1926c832007-04-24 00:23:05 +0000586/// UsualArithmeticConversions - Performs various conversions that are common to
587/// binary operators (C99 6.3.1.8). If both operands aren't arithmetic, this
588/// routine returns the first non-arithmetic type found. The client is
589/// responsible for emitting appropriate error diagnostics.
590QualType Sema::UsualArithmeticConversions(QualType t1, QualType t2) {
Steve Naroff71b59a92007-06-04 22:22:31 +0000591 QualType lhs = UsualUnaryConversions(t1);
592 QualType rhs = UsualUnaryConversions(t2);
Steve Naroff1926c832007-04-24 00:23:05 +0000593
Chris Lattner0c46c5d2007-06-02 23:53:17 +0000594 // If both types are identical, no conversion is needed.
595 if (lhs == rhs)
596 return lhs;
597
598 // If either side is a non-arithmetic type (e.g. a pointer), we are done.
599 // The caller can deal with this (e.g. pointer + int).
Steve Naroffb01bbe32007-04-25 01:22:31 +0000600 if (!lhs->isArithmeticType())
601 return lhs;
Steve Narofff633d092007-04-25 19:01:39 +0000602 if (!rhs->isArithmeticType())
Steve Naroffb01bbe32007-04-25 01:22:31 +0000603 return rhs;
Steve Naroff1926c832007-04-24 00:23:05 +0000604
Chris Lattner0c46c5d2007-06-02 23:53:17 +0000605 // At this point, we have two different arithmetic types.
Steve Naroffb01bbe32007-04-25 01:22:31 +0000606
607 // Handle complex types first (C99 6.3.1.8p1).
608 if (lhs->isComplexType() || rhs->isComplexType()) {
609 // if we have an integer operand, the result is the complex type.
610 if (rhs->isIntegerType())
611 return lhs;
612 if (lhs->isIntegerType())
613 return rhs;
614
Steve Naroffe4718892007-04-27 18:30:00 +0000615 return Context.maxComplexType(lhs, rhs);
Steve Naroffbf223ba2007-04-24 20:56:26 +0000616 }
Chris Lattner0c46c5d2007-06-02 23:53:17 +0000617
Steve Naroffb01bbe32007-04-25 01:22:31 +0000618 // Now handle "real" floating types (i.e. float, double, long double).
619 if (lhs->isRealFloatingType() || rhs->isRealFloatingType()) {
620 // if we have an integer operand, the result is the real floating type.
621 if (rhs->isIntegerType())
622 return lhs;
623 if (lhs->isIntegerType())
624 return rhs;
625
626 // we have two real floating types, float/complex combos were handled above.
Steve Naroffe4718892007-04-27 18:30:00 +0000627 return Context.maxFloatingType(lhs, rhs);
Steve Naroffb01bbe32007-04-25 01:22:31 +0000628 }
Steve Naroffe4718892007-04-27 18:30:00 +0000629 return Context.maxIntegerType(lhs, rhs);
Steve Narofff1e53692007-03-23 22:27:02 +0000630}
631
Steve Naroff3f597292007-05-11 22:18:03 +0000632// CheckPointerTypesForAssignment - This is a very tricky routine (despite
633// being closely modeled after the C99 spec:-). The odd characteristic of this
634// routine is it effectively iqnores the qualifiers on the top level pointee.
635// This circumvents the usual type rules specified in 6.2.7p1 & 6.7.5.[1-3].
636// FIXME: add a couple examples in this comment.
Steve Naroff98cf3e92007-06-06 18:38:38 +0000637Sema::AssignmentCheckResult
638Sema::CheckPointerTypesForAssignment(QualType lhsType, QualType rhsType) {
Steve Naroff3f597292007-05-11 22:18:03 +0000639 QualType lhptee, rhptee;
Steve Naroff1f4d7272007-05-11 04:00:31 +0000640
641 // get the "pointed to" type (ignoring qualifiers at the top level)
Steve Naroff3f597292007-05-11 22:18:03 +0000642 lhptee = cast<PointerType>(lhsType.getCanonicalType())->getPointeeType();
643 rhptee = cast<PointerType>(rhsType.getCanonicalType())->getPointeeType();
Steve Naroff1f4d7272007-05-11 04:00:31 +0000644
645 // make sure we operate on the canonical type
Steve Naroff3f597292007-05-11 22:18:03 +0000646 lhptee = lhptee.getCanonicalType();
647 rhptee = rhptee.getCanonicalType();
Steve Naroff1f4d7272007-05-11 04:00:31 +0000648
Steve Naroff98cf3e92007-06-06 18:38:38 +0000649 AssignmentCheckResult r = Compatible;
650
Steve Naroff3f597292007-05-11 22:18:03 +0000651 // C99 6.5.16.1p1: This following citation is common to constraints
652 // 3 & 4 (below). ...and the type *pointed to* by the left has all the
653 // qualifiers of the type *pointed to* by the right;
654 if ((lhptee.getQualifiers() & rhptee.getQualifiers()) !=
655 rhptee.getQualifiers())
656 r = CompatiblePointerDiscardsQualifiers;
657
658 // C99 6.5.16.1p1 (constraint 4): If one operand is a pointer to an object or
659 // incomplete type and the other is a pointer to a qualified or unqualified
660 // version of void...
661 if (lhptee.getUnqualifiedType()->isVoidType() &&
662 (rhptee->isObjectType() || rhptee->isIncompleteType()))
663 ;
664 else if (rhptee.getUnqualifiedType()->isVoidType() &&
665 (lhptee->isObjectType() || lhptee->isIncompleteType()))
666 ;
667 // C99 6.5.16.1p1 (constraint 3): both operands are pointers to qualified or
668 // unqualified versions of compatible types, ...
669 else if (!Type::typesAreCompatible(lhptee.getUnqualifiedType(),
670 rhptee.getUnqualifiedType()))
671 r = IncompatiblePointer; // this "trumps" PointerAssignDiscardsQualifiers
Steve Naroff98cf3e92007-06-06 18:38:38 +0000672 return r;
Steve Naroff1f4d7272007-05-11 04:00:31 +0000673}
674
Steve Naroff98cf3e92007-06-06 18:38:38 +0000675/// CheckAssignmentConstraints (C99 6.5.16) - This routine currently
Steve Naroff17f76e02007-05-03 21:03:48 +0000676/// has code to accommodate several GCC extensions when type checking
677/// pointers. Here are some objectionable examples that GCC considers warnings:
678///
679/// int a, *pint;
680/// short *pshort;
681/// struct foo *pfoo;
682///
683/// pint = pshort; // warning: assignment from incompatible pointer type
684/// a = pint; // warning: assignment makes integer from pointer without a cast
685/// pint = a; // warning: assignment makes pointer from integer without a cast
686/// pint = pfoo; // warning: assignment from incompatible pointer type
687///
688/// As a result, the code for dealing with pointers is more complex than the
689/// C99 spec dictates.
690/// Note: the warning above turn into errors when -pedantic-errors is enabled.
691///
Steve Naroff98cf3e92007-06-06 18:38:38 +0000692Sema::AssignmentCheckResult
693Sema::CheckAssignmentConstraints(QualType lhsType, QualType rhsType) {
Bill Wendling216423b2007-05-30 06:30:29 +0000694 // This check seems unnatural, however it is necessary to insure the proper
695 // conversion of functions/arrays. If the conversion were done for all
696 // DeclExpr's (created by ParseIdentifierExpr), it would mess up the unary
697 // expressions that surpress this implicit conversion (&, sizeof).
Steve Naroff71b59a92007-06-04 22:22:31 +0000698 rhsType = DefaultFunctionArrayConversion(rhsType);
Steve Naroffb891de32007-05-02 23:51:10 +0000699
Steve Naroff9eb24652007-05-02 21:58:15 +0000700 if (lhsType->isArithmeticType() && rhsType->isArithmeticType())
Steve Naroff98cf3e92007-06-06 18:38:38 +0000701 return Compatible;
Steve Naroff9eb24652007-05-02 21:58:15 +0000702 else if (lhsType->isPointerType()) {
Steve Naroff98cf3e92007-06-06 18:38:38 +0000703 if (rhsType->isIntegerType())
704 return PointerFromInt;
705
Steve Naroff3f597292007-05-11 22:18:03 +0000706 if (rhsType->isPointerType())
Steve Naroff98cf3e92007-06-06 18:38:38 +0000707 return CheckPointerTypesForAssignment(lhsType, rhsType);
Steve Naroff9eb24652007-05-02 21:58:15 +0000708 } else if (rhsType->isPointerType()) {
Steve Naroff98cf3e92007-06-06 18:38:38 +0000709 // C99 6.5.16.1p1: the left operand is _Bool and the right is a pointer.
710 if ((lhsType->isIntegerType()) && (lhsType != Context.BoolTy))
711 return IntFromPointer;
712
Steve Naroff3f597292007-05-11 22:18:03 +0000713 if (lhsType->isPointerType())
Steve Naroff98cf3e92007-06-06 18:38:38 +0000714 return CheckPointerTypesForAssignment(lhsType, rhsType);
Steve Naroff1f4d7272007-05-11 04:00:31 +0000715 } else if (isa<TagType>(lhsType) && isa<TagType>(rhsType)) {
716 if (Type::tagTypesAreCompatible(lhsType, rhsType))
Steve Naroff98cf3e92007-06-06 18:38:38 +0000717 return Compatible;
Bill Wendlingdb4f06e2007-06-02 23:29:59 +0000718 } else if (lhsType->isReferenceType() || rhsType->isReferenceType()) {
719 if (Type::referenceTypesAreCompatible(lhsType, rhsType))
Steve Naroff98cf3e92007-06-06 18:38:38 +0000720 return Compatible;
Bill Wendling216423b2007-05-30 06:30:29 +0000721 }
Steve Naroff98cf3e92007-06-06 18:38:38 +0000722 return Incompatible;
Steve Naroff9eb24652007-05-02 21:58:15 +0000723}
724
Steve Naroff6f49f5d2007-05-29 14:23:36 +0000725inline void Sema::InvalidOperands(SourceLocation loc, Expr *lex, Expr *rex) {
726 Diag(loc, diag::err_typecheck_invalid_operands,
727 lex->getType().getAsString(), rex->getType().getAsString(),
728 lex->getSourceRange(), rex->getSourceRange());
729}
730
Steve Naroff218bc2b2007-05-04 21:54:46 +0000731inline QualType Sema::CheckMultiplyDivideOperands(
732 Expr *lex, Expr *rex, SourceLocation loc)
Steve Naroff5c10d4b2007-04-20 23:42:24 +0000733{
Steve Naroff1926c832007-04-24 00:23:05 +0000734 QualType resType = UsualArithmeticConversions(lex->getType(), rex->getType());
Steve Naroff5c10d4b2007-04-20 23:42:24 +0000735
Steve Naroff218bc2b2007-05-04 21:54:46 +0000736 if (resType->isArithmeticType())
737 return resType;
Steve Naroff6f49f5d2007-05-29 14:23:36 +0000738 InvalidOperands(loc, lex, rex);
Steve Naroff218bc2b2007-05-04 21:54:46 +0000739 return QualType();
Steve Naroff26c8ea52007-03-21 21:08:52 +0000740}
741
Steve Naroff218bc2b2007-05-04 21:54:46 +0000742inline QualType Sema::CheckRemainderOperands(
743 Expr *lex, Expr *rex, SourceLocation loc)
744{
745 QualType resType = UsualArithmeticConversions(lex->getType(), rex->getType());
746
747 if (resType->isIntegerType())
748 return resType;
Steve Naroff6f49f5d2007-05-29 14:23:36 +0000749 InvalidOperands(loc, lex, rex);
Steve Naroff218bc2b2007-05-04 21:54:46 +0000750 return QualType();
751}
752
753inline QualType Sema::CheckAdditionOperands( // C99 6.5.6
754 Expr *lex, Expr *rex, SourceLocation loc)
Steve Naroff1926c832007-04-24 00:23:05 +0000755{
Steve Naroffe4718892007-04-27 18:30:00 +0000756 QualType lhsType = lex->getType(), rhsType = rex->getType();
757 QualType resType = UsualArithmeticConversions(lhsType, rhsType);
758
759 // handle the common case first (both operands are arithmetic).
760 if (resType->isArithmeticType())
Steve Naroff218bc2b2007-05-04 21:54:46 +0000761 return resType;
762
763 if ((lhsType->isPointerType() && rhsType->isIntegerType()) ||
764 (lhsType->isIntegerType() && rhsType->isPointerType()))
765 return resType;
Steve Naroff6f49f5d2007-05-29 14:23:36 +0000766 InvalidOperands(loc, lex, rex);
Steve Naroff218bc2b2007-05-04 21:54:46 +0000767 return QualType();
Steve Naroff26c8ea52007-03-21 21:08:52 +0000768}
769
Steve Naroff218bc2b2007-05-04 21:54:46 +0000770inline QualType Sema::CheckSubtractionOperands( // C99 6.5.6
771 Expr *lex, Expr *rex, SourceLocation loc)
772{
773 QualType lhsType = lex->getType(), rhsType = rex->getType();
774 QualType resType = UsualArithmeticConversions(lhsType, rhsType);
775
776 // handle the common case first (both operands are arithmetic).
777 if (resType->isArithmeticType())
778 return resType;
779 if ((lhsType->isPointerType() && rhsType->isIntegerType()) ||
780 (lhsType->isPointerType() && rhsType->isPointerType()))
781 return resType;
Steve Naroff6f49f5d2007-05-29 14:23:36 +0000782 InvalidOperands(loc, lex, rex);
Steve Naroff218bc2b2007-05-04 21:54:46 +0000783 return QualType();
784}
785
786inline QualType Sema::CheckShiftOperands( // C99 6.5.7
787 Expr *lex, Expr *rex, SourceLocation loc)
Steve Naroff1926c832007-04-24 00:23:05 +0000788{
Chris Lattner1d411a82007-06-05 20:52:21 +0000789 // FIXME: Shifts don't perform usual arithmetic conversions. This is wrong
790 // for int << longlong -> the result type should be int, not long long.
Steve Naroff1926c832007-04-24 00:23:05 +0000791 QualType resType = UsualArithmeticConversions(lex->getType(), rex->getType());
792
Steve Naroff218bc2b2007-05-04 21:54:46 +0000793 if (resType->isIntegerType())
794 return resType;
Steve Naroff6f49f5d2007-05-29 14:23:36 +0000795 InvalidOperands(loc, lex, rex);
Steve Naroff218bc2b2007-05-04 21:54:46 +0000796 return QualType();
Steve Naroff26c8ea52007-03-21 21:08:52 +0000797}
798
Steve Naroff218bc2b2007-05-04 21:54:46 +0000799inline QualType Sema::CheckRelationalOperands( // C99 6.5.8
800 Expr *lex, Expr *rex, SourceLocation loc)
Steve Naroff1926c832007-04-24 00:23:05 +0000801{
802 QualType lType = lex->getType(), rType = rex->getType();
803
804 if (lType->isRealType() && rType->isRealType())
Steve Naroff218bc2b2007-05-04 21:54:46 +0000805 return Context.IntTy;
Steve Naroffe4718892007-04-27 18:30:00 +0000806
807 if (lType->isPointerType() && rType->isPointerType())
Steve Naroff218bc2b2007-05-04 21:54:46 +0000808 return Context.IntTy;
Steve Naroffe4718892007-04-27 18:30:00 +0000809
Steve Naroff043d45d2007-05-15 02:32:35 +0000810 if (lType->isIntegerType() || rType->isIntegerType()) { // GCC extension.
Steve Naroff218bc2b2007-05-04 21:54:46 +0000811 Diag(loc, diag::ext_typecheck_comparison_of_pointer_integer);
Steve Naroff043d45d2007-05-15 02:32:35 +0000812 return Context.IntTy;
813 }
Steve Naroff6f49f5d2007-05-29 14:23:36 +0000814 InvalidOperands(loc, lex, rex);
Steve Naroff218bc2b2007-05-04 21:54:46 +0000815 return QualType();
Steve Naroff26c8ea52007-03-21 21:08:52 +0000816}
817
Steve Naroff218bc2b2007-05-04 21:54:46 +0000818inline QualType Sema::CheckEqualityOperands( // C99 6.5.9
819 Expr *lex, Expr *rex, SourceLocation loc)
Steve Naroff1926c832007-04-24 00:23:05 +0000820{
821 QualType lType = lex->getType(), rType = rex->getType();
822
823 if (lType->isArithmeticType() && rType->isArithmeticType())
Steve Naroff218bc2b2007-05-04 21:54:46 +0000824 return Context.IntTy;
Steve Naroffe4718892007-04-27 18:30:00 +0000825 if (lType->isPointerType() && rType->isPointerType())
Steve Naroff218bc2b2007-05-04 21:54:46 +0000826 return Context.IntTy;
827
Steve Naroff043d45d2007-05-15 02:32:35 +0000828 if (lType->isIntegerType() || rType->isIntegerType()) { // GCC extension.
Steve Naroff218bc2b2007-05-04 21:54:46 +0000829 Diag(loc, diag::ext_typecheck_comparison_of_pointer_integer);
Steve Naroff043d45d2007-05-15 02:32:35 +0000830 return Context.IntTy;
831 }
Steve Naroff6f49f5d2007-05-29 14:23:36 +0000832 InvalidOperands(loc, lex, rex);
Steve Naroff218bc2b2007-05-04 21:54:46 +0000833 return QualType();
Steve Naroff26c8ea52007-03-21 21:08:52 +0000834}
835
Steve Naroff218bc2b2007-05-04 21:54:46 +0000836inline QualType Sema::CheckBitwiseOperands(
837 Expr *lex, Expr *rex, SourceLocation loc)
Steve Naroff1926c832007-04-24 00:23:05 +0000838{
839 QualType resType = UsualArithmeticConversions(lex->getType(), rex->getType());
840
Steve Naroff218bc2b2007-05-04 21:54:46 +0000841 if (resType->isIntegerType())
842 return resType;
Steve Naroff6f49f5d2007-05-29 14:23:36 +0000843 InvalidOperands(loc, lex, rex);
Steve Naroff218bc2b2007-05-04 21:54:46 +0000844 return QualType();
Steve Naroff26c8ea52007-03-21 21:08:52 +0000845}
846
Steve Naroff218bc2b2007-05-04 21:54:46 +0000847inline QualType Sema::CheckLogicalOperands( // C99 6.5.[13,14]
848 Expr *lex, Expr *rex, SourceLocation loc)
Steve Naroff1926c832007-04-24 00:23:05 +0000849{
Steve Naroff71b59a92007-06-04 22:22:31 +0000850 QualType lhsType = UsualUnaryConversions(lex->getType());
851 QualType rhsType = UsualUnaryConversions(rex->getType());
Steve Naroffe4718892007-04-27 18:30:00 +0000852
Steve Naroff218bc2b2007-05-04 21:54:46 +0000853 if (lhsType->isScalarType() || rhsType->isScalarType())
854 return Context.IntTy;
Steve Naroff6f49f5d2007-05-29 14:23:36 +0000855 InvalidOperands(loc, lex, rex);
Steve Naroff218bc2b2007-05-04 21:54:46 +0000856 return QualType();
Steve Naroffae4143e2007-04-26 20:39:23 +0000857}
858
Steve Naroff35d85152007-05-07 00:24:15 +0000859inline QualType Sema::CheckAssignmentOperands( // C99 6.5.16.1
860 Expr *lex, Expr *rex, SourceLocation loc, QualType compoundType)
Steve Naroffae4143e2007-04-26 20:39:23 +0000861{
Steve Naroff0af91202007-04-27 21:51:21 +0000862 QualType lhsType = lex->getType();
Steve Naroff35d85152007-05-07 00:24:15 +0000863 QualType rhsType = compoundType.isNull() ? rex->getType() : compoundType;
Steve Naroff1f4d7272007-05-11 04:00:31 +0000864 bool hadError = false;
Steve Naroff9358c712007-05-27 23:58:33 +0000865 Expr::isModifiableLvalueResult mlval = lex->isModifiableLvalue();
866
867 switch (mlval) { // C99 6.5.16p2
868 case Expr::MLV_Valid:
869 break;
870 case Expr::MLV_ConstQualified:
871 Diag(loc, diag::err_typecheck_assign_const, lex->getSourceRange());
872 hadError = true;
873 break;
874 case Expr::MLV_ArrayType:
875 Diag(loc, diag::err_typecheck_array_not_modifiable_lvalue,
876 lhsType.getAsString(), lex->getSourceRange());
877 return QualType();
878 case Expr::MLV_NotObjectType:
879 Diag(loc, diag::err_typecheck_non_object_not_modifiable_lvalue,
880 lhsType.getAsString(), lex->getSourceRange());
881 return QualType();
882 case Expr::MLV_InvalidExpression:
883 Diag(loc, diag::err_typecheck_expression_not_modifiable_lvalue,
884 lex->getSourceRange());
885 return QualType();
886 case Expr::MLV_IncompleteType:
887 case Expr::MLV_IncompleteVoidType:
888 Diag(loc, diag::err_typecheck_incomplete_type_not_modifiable_lvalue,
889 lhsType.getAsString(), lex->getSourceRange());
890 return QualType();
Steve Naroff218bc2b2007-05-04 21:54:46 +0000891 }
892 if (lhsType == rhsType) // common case, fast path...
893 return lhsType;
894
Steve Naroff98cf3e92007-06-06 18:38:38 +0000895 AssignmentCheckResult result = CheckAssignmentConstraints(lhsType, rhsType);
Steve Naroff218bc2b2007-05-04 21:54:46 +0000896
897 // decode the result (notice that extensions still return a type).
898 switch (result) {
899 case Compatible:
Steve Naroff1f4d7272007-05-11 04:00:31 +0000900 break;
Steve Naroff218bc2b2007-05-04 21:54:46 +0000901 case Incompatible:
Steve Naroffe845e272007-05-18 01:06:45 +0000902 Diag(loc, diag::err_typecheck_assign_incompatible,
Chris Lattner84e160a2007-05-19 07:03:17 +0000903 lhsType.getAsString(), rhsType.getAsString(),
904 lex->getSourceRange(), rex->getSourceRange());
Steve Naroff1f4d7272007-05-11 04:00:31 +0000905 hadError = true;
906 break;
Steve Naroff218bc2b2007-05-04 21:54:46 +0000907 case PointerFromInt:
908 // check for null pointer constant (C99 6.3.2.3p3)
Steve Naroff9992bba2007-05-30 16:27:15 +0000909 if (compoundType.isNull() && !rex->isNullPointerConstant()) {
Steve Naroff56faab22007-05-30 04:20:12 +0000910 Diag(loc, diag::ext_typecheck_assign_pointer_int,
911 lhsType.getAsString(), rhsType.getAsString(),
Steve Naroff9358c712007-05-27 23:58:33 +0000912 lex->getSourceRange(), rex->getSourceRange());
Steve Naroff9992bba2007-05-30 16:27:15 +0000913 }
Steve Naroff1f4d7272007-05-11 04:00:31 +0000914 break;
Steve Naroff56faab22007-05-30 04:20:12 +0000915 case IntFromPointer:
916 Diag(loc, diag::ext_typecheck_assign_pointer_int,
917 lhsType.getAsString(), rhsType.getAsString(),
Steve Naroff9358c712007-05-27 23:58:33 +0000918 lex->getSourceRange(), rex->getSourceRange());
Steve Naroff1f4d7272007-05-11 04:00:31 +0000919 break;
Steve Naroff218bc2b2007-05-04 21:54:46 +0000920 case IncompatiblePointer:
Steve Naroff9358c712007-05-27 23:58:33 +0000921 Diag(loc, diag::ext_typecheck_assign_incompatible_pointer,
922 lhsType.getAsString(), rhsType.getAsString(),
923 lex->getSourceRange(), rex->getSourceRange());
Steve Naroff1f4d7272007-05-11 04:00:31 +0000924 break;
925 case CompatiblePointerDiscardsQualifiers:
Steve Naroff9358c712007-05-27 23:58:33 +0000926 Diag(loc, diag::ext_typecheck_assign_discards_qualifiers,
927 lhsType.getAsString(), rhsType.getAsString(),
928 lex->getSourceRange(), rex->getSourceRange());
Steve Naroff1f4d7272007-05-11 04:00:31 +0000929 break;
Steve Naroff218bc2b2007-05-04 21:54:46 +0000930 }
Steve Naroff98cf3e92007-06-06 18:38:38 +0000931 // C99 6.5.16p3: The type of an assignment expression is the type of the
932 // left operand unless the left operand has qualified type, in which case
933 // it is the unqualified version of the type of the left operand.
934 // C99 6.5.16.1p2: In simple assignment, the value of the right operand
935 // is converted to the type of the assignment expression (above).
936 // C++ 5.17p1: the type of the assignment expression is that of its left oprdu.
937 return hadError ? QualType() : lhsType.getUnqualifiedType();
Steve Naroffae4143e2007-04-26 20:39:23 +0000938}
939
Steve Naroff218bc2b2007-05-04 21:54:46 +0000940inline QualType Sema::CheckCommaOperands( // C99 6.5.17
Chris Lattner84e160a2007-05-19 07:03:17 +0000941 Expr *lex, Expr *rex, SourceLocation loc) {
Steve Naroff71b59a92007-06-04 22:22:31 +0000942 return UsualUnaryConversions(rex->getType());
Steve Naroff95af0132007-03-30 23:47:58 +0000943}
944
Steve Naroff71ce2e02007-05-18 22:53:50 +0000945QualType Sema::CheckIncrementDecrementOperand(Expr *op, SourceLocation OpLoc) {
Steve Naroff094046f2007-05-13 03:21:25 +0000946 QualType resType = UsualArithmeticConversions(op->getType(), Context.IntTy);
Steve Naroff35d85152007-05-07 00:24:15 +0000947 assert(!resType.isNull() && "no type for increment/decrement expression");
Steve Naroffd50c88e2007-04-05 21:15:20 +0000948
Steve Naroff46ba1eb2007-04-03 23:13:13 +0000949 // C99 6.5.2.4p1
Steve Naroff35d85152007-05-07 00:24:15 +0000950 if (const PointerType *pt = dyn_cast<PointerType>(resType)) {
951 if (!pt->getPointeeType()->isObjectType()) { // C99 6.5.2.4p2, 6.5.6p2
Steve Naroff71ce2e02007-05-18 22:53:50 +0000952 Diag(OpLoc, diag::err_typecheck_arithmetic_incomplete_type,
953 resType.getAsString(), op->getSourceRange());
Steve Naroff35d85152007-05-07 00:24:15 +0000954 return QualType();
955 }
956 } else if (!resType->isRealType()) {
Steve Naroff95af0132007-03-30 23:47:58 +0000957 // FIXME: Allow Complex as a GCC extension.
Steve Naroff71ce2e02007-05-18 22:53:50 +0000958 Diag(OpLoc, diag::err_typecheck_illegal_increment_decrement,
959 resType.getAsString(), op->getSourceRange());
Steve Naroff35d85152007-05-07 00:24:15 +0000960 return QualType();
Steve Naroff46ba1eb2007-04-03 23:13:13 +0000961 }
Steve Naroff094046f2007-05-13 03:21:25 +0000962 // At this point, we know we have a real or pointer type. Now make sure
963 // the operand is a modifiable lvalue.
Steve Naroff9358c712007-05-27 23:58:33 +0000964 Expr::isModifiableLvalueResult mlval = op->isModifiableLvalue();
965 if (mlval != Expr::MLV_Valid) {
966 // FIXME: emit a more precise diagnostic...
Steve Naroff71ce2e02007-05-18 22:53:50 +0000967 Diag(OpLoc, diag::err_typecheck_invalid_lvalue_incr_decr,
Chris Lattner84e160a2007-05-19 07:03:17 +0000968 op->getSourceRange());
Steve Naroff35d85152007-05-07 00:24:15 +0000969 return QualType();
970 }
971 return resType;
Steve Naroff26c8ea52007-03-21 21:08:52 +0000972}
973
Steve Naroff1926c832007-04-24 00:23:05 +0000974/// getPrimaryDeclaration - Helper function for CheckAddressOfOperand().
Steve Naroff47500512007-04-19 23:00:49 +0000975/// This routine allows us to typecheck complex/recursive expressions
976/// where the declaration is needed for type checking. Here are some
977/// examples: &s.xx, &s.zz[1].yy, &(1+2), &(XX), &"123"[2].
Steve Naroff1926c832007-04-24 00:23:05 +0000978static Decl *getPrimaryDeclaration(Expr *e) {
Steve Naroff47500512007-04-19 23:00:49 +0000979 switch (e->getStmtClass()) {
980 case Stmt::DeclRefExprClass:
981 return cast<DeclRefExpr>(e)->getDecl();
982 case Stmt::MemberExprClass:
Steve Naroff1926c832007-04-24 00:23:05 +0000983 return getPrimaryDeclaration(cast<MemberExpr>(e)->getBase());
Steve Naroff47500512007-04-19 23:00:49 +0000984 case Stmt::ArraySubscriptExprClass:
Steve Naroff1926c832007-04-24 00:23:05 +0000985 return getPrimaryDeclaration(cast<ArraySubscriptExpr>(e)->getBase());
Steve Naroff47500512007-04-19 23:00:49 +0000986 case Stmt::CallExprClass:
Steve Naroff1926c832007-04-24 00:23:05 +0000987 return getPrimaryDeclaration(cast<CallExpr>(e)->getCallee());
Steve Naroff47500512007-04-19 23:00:49 +0000988 case Stmt::UnaryOperatorClass:
Steve Naroff1926c832007-04-24 00:23:05 +0000989 return getPrimaryDeclaration(cast<UnaryOperator>(e)->getSubExpr());
Steve Naroff47500512007-04-19 23:00:49 +0000990 case Stmt::ParenExprClass:
Steve Naroff1926c832007-04-24 00:23:05 +0000991 return getPrimaryDeclaration(cast<ParenExpr>(e)->getSubExpr());
Steve Naroff47500512007-04-19 23:00:49 +0000992 default:
993 return 0;
994 }
995}
996
997/// CheckAddressOfOperand - The operand of & must be either a function
998/// designator or an lvalue designating an object. If it is an lvalue, the
999/// object cannot be declared with storage class register or be a bit field.
1000/// Note: The usual conversions are *not* applied to the operand of the &
Steve Naroffa78fe7e2007-05-16 19:47:19 +00001001/// operator (C99 6.3.2.1p[2-4]), and its result is never an lvalue.
Steve Naroff35d85152007-05-07 00:24:15 +00001002QualType Sema::CheckAddressOfOperand(Expr *op, SourceLocation OpLoc) {
Steve Naroff1926c832007-04-24 00:23:05 +00001003 Decl *dcl = getPrimaryDeclaration(op);
Steve Naroff9358c712007-05-27 23:58:33 +00001004 Expr::isLvalueResult lval = op->isLvalue();
Steve Naroff47500512007-04-19 23:00:49 +00001005
Steve Naroff9358c712007-05-27 23:58:33 +00001006 if (lval != Expr::LV_Valid) { // C99 6.5.3.2p1
Steve Naroff5dd642e2007-05-14 18:14:51 +00001007 if (dcl && isa<FunctionDecl>(dcl)) // allow function designators
1008 ;
Steve Naroff9358c712007-05-27 23:58:33 +00001009 else { // FIXME: emit more specific diag...
Steve Naroff71ce2e02007-05-18 22:53:50 +00001010 Diag(OpLoc, diag::err_typecheck_invalid_lvalue_addrof,
1011 op->getSourceRange());
Steve Naroff35d85152007-05-07 00:24:15 +00001012 return QualType();
1013 }
Steve Naroff47500512007-04-19 23:00:49 +00001014 } else if (dcl) {
1015 // We have an lvalue with a decl. Make sure the decl is not declared
1016 // with the register storage-class specifier.
1017 if (const VarDecl *vd = dyn_cast<VarDecl>(dcl)) {
Steve Naroff35d85152007-05-07 00:24:15 +00001018 if (vd->getStorageClass() == VarDecl::Register) {
Steve Naroff71ce2e02007-05-18 22:53:50 +00001019 Diag(OpLoc, diag::err_typecheck_address_of_register,
Chris Lattner84e160a2007-05-19 07:03:17 +00001020 op->getSourceRange());
Steve Naroff35d85152007-05-07 00:24:15 +00001021 return QualType();
1022 }
Steve Narofff633d092007-04-25 19:01:39 +00001023 } else
1024 assert(0 && "Unknown/unexpected decl type");
1025
Steve Naroff47500512007-04-19 23:00:49 +00001026 // FIXME: add check for bitfields!
1027 }
1028 // If the operand has type "type", the result has type "pointer to type".
Steve Naroff35d85152007-05-07 00:24:15 +00001029 return Context.getPointerType(op->getType());
Steve Naroff47500512007-04-19 23:00:49 +00001030}
1031
Steve Naroff35d85152007-05-07 00:24:15 +00001032QualType Sema::CheckIndirectionOperand(Expr *op, SourceLocation OpLoc) {
Steve Naroff71b59a92007-06-04 22:22:31 +00001033 QualType qType = UsualUnaryConversions(op->getType());
Steve Naroff35d85152007-05-07 00:24:15 +00001034
Steve Naroff47500512007-04-19 23:00:49 +00001035 assert(!qType.isNull() && "no type for * expression");
1036
Steve Naroff758ada12007-05-28 16:15:57 +00001037 if (PointerType *PT = dyn_cast<PointerType>(qType.getCanonicalType())) {
1038 QualType ptype = PT->getPointeeType();
1039 // C99 6.5.3.2p4. "if it points to an object,...".
1040 if (ptype->isIncompleteType()) { // An incomplete type is not an object
1041 // GCC compat: special case 'void *' (treat as warning).
1042 if (ptype->isVoidType()) {
1043 Diag(OpLoc, diag::ext_typecheck_deref_ptr_to_void,
Steve Naroffeb9da942007-05-30 00:06:37 +00001044 qType.getAsString(), op->getSourceRange());
Steve Naroff758ada12007-05-28 16:15:57 +00001045 } else {
1046 Diag(OpLoc, diag::err_typecheck_deref_incomplete_type,
Steve Naroffeb9da942007-05-30 00:06:37 +00001047 ptype.getAsString(), op->getSourceRange());
Steve Naroff758ada12007-05-28 16:15:57 +00001048 return QualType();
1049 }
1050 }
1051 return ptype;
1052 }
1053 Diag(OpLoc, diag::err_typecheck_indirection_requires_pointer,
Steve Naroffeb9da942007-05-30 00:06:37 +00001054 qType.getAsString(), op->getSourceRange());
Steve Naroff35d85152007-05-07 00:24:15 +00001055 return QualType();
Steve Naroff1926c832007-04-24 00:23:05 +00001056}
Steve Naroff218bc2b2007-05-04 21:54:46 +00001057
1058static inline BinaryOperator::Opcode ConvertTokenKindToBinaryOpcode(
1059 tok::TokenKind Kind) {
1060 BinaryOperator::Opcode Opc;
1061 switch (Kind) {
1062 default: assert(0 && "Unknown binop!");
1063 case tok::star: Opc = BinaryOperator::Mul; break;
1064 case tok::slash: Opc = BinaryOperator::Div; break;
1065 case tok::percent: Opc = BinaryOperator::Rem; break;
1066 case tok::plus: Opc = BinaryOperator::Add; break;
1067 case tok::minus: Opc = BinaryOperator::Sub; break;
1068 case tok::lessless: Opc = BinaryOperator::Shl; break;
1069 case tok::greatergreater: Opc = BinaryOperator::Shr; break;
1070 case tok::lessequal: Opc = BinaryOperator::LE; break;
1071 case tok::less: Opc = BinaryOperator::LT; break;
1072 case tok::greaterequal: Opc = BinaryOperator::GE; break;
1073 case tok::greater: Opc = BinaryOperator::GT; break;
1074 case tok::exclaimequal: Opc = BinaryOperator::NE; break;
1075 case tok::equalequal: Opc = BinaryOperator::EQ; break;
1076 case tok::amp: Opc = BinaryOperator::And; break;
1077 case tok::caret: Opc = BinaryOperator::Xor; break;
1078 case tok::pipe: Opc = BinaryOperator::Or; break;
1079 case tok::ampamp: Opc = BinaryOperator::LAnd; break;
1080 case tok::pipepipe: Opc = BinaryOperator::LOr; break;
1081 case tok::equal: Opc = BinaryOperator::Assign; break;
1082 case tok::starequal: Opc = BinaryOperator::MulAssign; break;
1083 case tok::slashequal: Opc = BinaryOperator::DivAssign; break;
1084 case tok::percentequal: Opc = BinaryOperator::RemAssign; break;
1085 case tok::plusequal: Opc = BinaryOperator::AddAssign; break;
1086 case tok::minusequal: Opc = BinaryOperator::SubAssign; break;
1087 case tok::lesslessequal: Opc = BinaryOperator::ShlAssign; break;
1088 case tok::greatergreaterequal: Opc = BinaryOperator::ShrAssign; break;
1089 case tok::ampequal: Opc = BinaryOperator::AndAssign; break;
1090 case tok::caretequal: Opc = BinaryOperator::XorAssign; break;
1091 case tok::pipeequal: Opc = BinaryOperator::OrAssign; break;
1092 case tok::comma: Opc = BinaryOperator::Comma; break;
1093 }
1094 return Opc;
1095}
1096
Steve Naroff35d85152007-05-07 00:24:15 +00001097static inline UnaryOperator::Opcode ConvertTokenKindToUnaryOpcode(
1098 tok::TokenKind Kind) {
1099 UnaryOperator::Opcode Opc;
1100 switch (Kind) {
1101 default: assert(0 && "Unknown unary op!");
1102 case tok::plusplus: Opc = UnaryOperator::PreInc; break;
1103 case tok::minusminus: Opc = UnaryOperator::PreDec; break;
1104 case tok::amp: Opc = UnaryOperator::AddrOf; break;
1105 case tok::star: Opc = UnaryOperator::Deref; break;
1106 case tok::plus: Opc = UnaryOperator::Plus; break;
1107 case tok::minus: Opc = UnaryOperator::Minus; break;
1108 case tok::tilde: Opc = UnaryOperator::Not; break;
1109 case tok::exclaim: Opc = UnaryOperator::LNot; break;
1110 case tok::kw_sizeof: Opc = UnaryOperator::SizeOf; break;
1111 case tok::kw___alignof: Opc = UnaryOperator::AlignOf; break;
1112 case tok::kw___real: Opc = UnaryOperator::Real; break;
1113 case tok::kw___imag: Opc = UnaryOperator::Imag; break;
Steve Naroff35d85152007-05-07 00:24:15 +00001114 // FIXME: case tok::kw___extension__:
1115 }
1116 return Opc;
1117}
1118
Steve Naroff218bc2b2007-05-04 21:54:46 +00001119// Binary Operators. 'Tok' is the token for the operator.
1120Action::ExprResult Sema::ParseBinOp(SourceLocation TokLoc, tok::TokenKind Kind,
1121 ExprTy *LHS, ExprTy *RHS) {
1122 BinaryOperator::Opcode Opc = ConvertTokenKindToBinaryOpcode(Kind);
1123 Expr *lhs = (Expr *)LHS, *rhs = (Expr*)RHS;
1124
1125 assert((lhs != 0) && "ParseBinOp(): missing left expression");
1126 assert((rhs != 0) && "ParseBinOp(): missing right expression");
1127
1128 QualType result;
1129
1130 switch (Opc) {
1131 default:
1132 assert(0 && "Unknown binary expr!");
1133 case BinaryOperator::Assign:
Steve Naroff35d85152007-05-07 00:24:15 +00001134 result = CheckAssignmentOperands(lhs, rhs, TokLoc, QualType());
Steve Naroff218bc2b2007-05-04 21:54:46 +00001135 break;
1136 case BinaryOperator::Mul:
1137 case BinaryOperator::Div:
1138 result = CheckMultiplyDivideOperands(lhs, rhs, TokLoc);
1139 break;
1140 case BinaryOperator::Rem:
1141 result = CheckRemainderOperands(lhs, rhs, TokLoc);
1142 break;
1143 case BinaryOperator::Add:
1144 result = CheckAdditionOperands(lhs, rhs, TokLoc);
1145 break;
1146 case BinaryOperator::Sub:
1147 result = CheckSubtractionOperands(lhs, rhs, TokLoc);
1148 break;
1149 case BinaryOperator::Shl:
1150 case BinaryOperator::Shr:
1151 result = CheckShiftOperands(lhs, rhs, TokLoc);
1152 break;
1153 case BinaryOperator::LE:
1154 case BinaryOperator::LT:
1155 case BinaryOperator::GE:
1156 case BinaryOperator::GT:
1157 result = CheckRelationalOperands(lhs, rhs, TokLoc);
1158 break;
1159 case BinaryOperator::EQ:
1160 case BinaryOperator::NE:
1161 result = CheckEqualityOperands(lhs, rhs, TokLoc);
1162 break;
1163 case BinaryOperator::And:
1164 case BinaryOperator::Xor:
1165 case BinaryOperator::Or:
1166 result = CheckBitwiseOperands(lhs, rhs, TokLoc);
1167 break;
1168 case BinaryOperator::LAnd:
1169 case BinaryOperator::LOr:
1170 result = CheckLogicalOperands(lhs, rhs, TokLoc);
1171 break;
1172 case BinaryOperator::MulAssign:
1173 case BinaryOperator::DivAssign:
1174 result = CheckMultiplyDivideOperands(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::RemAssign:
1180 result = CheckRemainderOperands(lhs, rhs, TokLoc);
1181 if (result.isNull())
1182 return true;
Steve Naroff35d85152007-05-07 00:24:15 +00001183 result = CheckAssignmentOperands(lhs, rhs, TokLoc, result);
Steve Naroff218bc2b2007-05-04 21:54:46 +00001184 break;
1185 case BinaryOperator::AddAssign:
1186 result = CheckAdditionOperands(lhs, rhs, TokLoc);
1187 if (result.isNull())
1188 return true;
Steve Naroff35d85152007-05-07 00:24:15 +00001189 result = CheckAssignmentOperands(lhs, rhs, TokLoc, result);
Steve Naroff218bc2b2007-05-04 21:54:46 +00001190 break;
1191 case BinaryOperator::SubAssign:
1192 result = CheckSubtractionOperands(lhs, rhs, TokLoc);
1193 if (result.isNull())
1194 return true;
Steve Naroff35d85152007-05-07 00:24:15 +00001195 result = CheckAssignmentOperands(lhs, rhs, TokLoc, result);
Steve Naroff218bc2b2007-05-04 21:54:46 +00001196 break;
1197 case BinaryOperator::ShlAssign:
1198 case BinaryOperator::ShrAssign:
1199 result = CheckShiftOperands(lhs, rhs, TokLoc);
1200 if (result.isNull())
1201 return true;
Steve Naroff35d85152007-05-07 00:24:15 +00001202 result = CheckAssignmentOperands(lhs, rhs, TokLoc, result);
Steve Naroff218bc2b2007-05-04 21:54:46 +00001203 break;
1204 case BinaryOperator::AndAssign:
1205 case BinaryOperator::XorAssign:
1206 case BinaryOperator::OrAssign:
1207 result = CheckBitwiseOperands(lhs, rhs, TokLoc);
1208 if (result.isNull())
1209 return true;
Steve Naroff35d85152007-05-07 00:24:15 +00001210 result = CheckAssignmentOperands(lhs, rhs, TokLoc, result);
Steve Naroff218bc2b2007-05-04 21:54:46 +00001211 break;
1212 case BinaryOperator::Comma:
1213 result = CheckCommaOperands(lhs, rhs, TokLoc);
1214 break;
1215 }
1216 if (result.isNull())
1217 return true;
1218 return new BinaryOperator(lhs, rhs, Opc, result);
1219}
1220
Steve Naroff35d85152007-05-07 00:24:15 +00001221// Unary Operators. 'Tok' is the token for the operator.
1222Action::ExprResult Sema::ParseUnaryOp(SourceLocation OpLoc, tok::TokenKind Op,
1223 ExprTy *Input) {
1224 UnaryOperator::Opcode Opc = ConvertTokenKindToUnaryOpcode(Op);
1225 QualType resultType;
1226 switch (Opc) {
1227 default:
1228 assert(0 && "Unimplemented unary expr!");
1229 case UnaryOperator::PreInc:
1230 case UnaryOperator::PreDec:
Steve Naroff71ce2e02007-05-18 22:53:50 +00001231 resultType = CheckIncrementDecrementOperand((Expr *)Input, OpLoc);
Steve Naroff35d85152007-05-07 00:24:15 +00001232 break;
1233 case UnaryOperator::AddrOf:
1234 resultType = CheckAddressOfOperand((Expr *)Input, OpLoc);
1235 break;
1236 case UnaryOperator::Deref:
1237 resultType = CheckIndirectionOperand((Expr *)Input, OpLoc);
1238 break;
1239 case UnaryOperator::Plus:
1240 case UnaryOperator::Minus:
Steve Naroff71b59a92007-06-04 22:22:31 +00001241 resultType = UsualUnaryConversions(((Expr *)Input)->getType());
Steve Naroff35d85152007-05-07 00:24:15 +00001242 if (!resultType->isArithmeticType()) // C99 6.5.3.3p1
Chris Lattnerc04bd6a2007-05-16 18:09:54 +00001243 return Diag(OpLoc, diag::err_typecheck_unary_expr,
1244 resultType.getAsString());
Steve Naroff35d85152007-05-07 00:24:15 +00001245 break;
1246 case UnaryOperator::Not: // bitwise complement
Steve Naroff71b59a92007-06-04 22:22:31 +00001247 resultType = UsualUnaryConversions(((Expr *)Input)->getType());
Steve Naroff35d85152007-05-07 00:24:15 +00001248 if (!resultType->isIntegerType()) // C99 6.5.3.3p1
Chris Lattnerc04bd6a2007-05-16 18:09:54 +00001249 return Diag(OpLoc, diag::err_typecheck_unary_expr,
1250 resultType.getAsString());
Steve Naroff35d85152007-05-07 00:24:15 +00001251 break;
1252 case UnaryOperator::LNot: // logical negation
Steve Naroff71b59a92007-06-04 22:22:31 +00001253 // Unlike +/-/~, integer promotions aren't done here (C99 6.5.3.3p5).
1254 resultType = DefaultFunctionArrayConversion(((Expr *)Input)->getType());
Steve Naroff35d85152007-05-07 00:24:15 +00001255 if (!resultType->isScalarType()) // C99 6.5.3.3p1
Chris Lattnerc04bd6a2007-05-16 18:09:54 +00001256 return Diag(OpLoc, diag::err_typecheck_unary_expr,
1257 resultType.getAsString());
Chris Lattnerbe31ed82007-06-02 19:11:33 +00001258 // LNot always has type int. C99 6.5.3.3p5.
1259 resultType = Context.IntTy;
Steve Naroff35d85152007-05-07 00:24:15 +00001260 break;
1261 case UnaryOperator::SizeOf:
Steve Naroff043d45d2007-05-15 02:32:35 +00001262 resultType = CheckSizeOfAlignOfOperand(((Expr *)Input)->getType(), OpLoc,
1263 true);
1264 break;
Steve Naroff35d85152007-05-07 00:24:15 +00001265 case UnaryOperator::AlignOf:
Steve Naroff043d45d2007-05-15 02:32:35 +00001266 resultType = CheckSizeOfAlignOfOperand(((Expr *)Input)->getType(), OpLoc,
1267 false);
1268 break;
Steve Naroff35d85152007-05-07 00:24:15 +00001269 }
1270 if (resultType.isNull())
1271 return true;
Steve Naroff509fe022007-05-17 01:16:00 +00001272 return new UnaryOperator((Expr *)Input, Opc, resultType, OpLoc);
Steve Naroff35d85152007-05-07 00:24:15 +00001273}
Chris Lattnereefa10e2007-05-28 06:56:27 +00001274
1275/// ParseAddrLabel - Parse the GNU address of label extension: "&&foo".
1276Sema::ExprResult Sema::ParseAddrLabel(SourceLocation OpLoc,
1277 SourceLocation LabLoc,
1278 IdentifierInfo *LabelII) {
1279 // Look up the record for this label identifier.
1280 LabelStmt *&LabelDecl = LabelMap[LabelII];
1281
1282 // If we haven't seen this label yet, create a forward reference.
1283 if (LabelDecl == 0)
1284 LabelDecl = new LabelStmt(LabLoc, LabelII, 0);
1285
1286 // Create the AST node. The address of a label always has type 'void*'.
1287 return new AddrLabel(OpLoc, LabLoc, LabelDecl,
1288 Context.getPointerType(Context.VoidTy));
1289}
1290