blob: f86a68303e277d503c04de8b9bb96102756dad26 [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 clang;
26
Steve Naroffdf7855b2007-02-21 23:46:25 +000027/// ParseStringLiteral - The specified tokens were lexed as pasted string
Chris Lattner5b183d82006-11-10 05:03:26 +000028/// fragments (e.g. "foo" "bar" L"baz"). The result string has to handle string
29/// concatenation ([C99 5.1.1.2, translation phase #6]), so it may come from
30/// multiple tokens. However, the common case is that StringToks points to one
31/// string.
32///
33Action::ExprResult
Steve Naroffdf7855b2007-02-21 23:46:25 +000034Sema::ParseStringLiteral(const LexerToken *StringToks, unsigned NumStringToks) {
Chris Lattner5b183d82006-11-10 05:03:26 +000035 assert(NumStringToks && "Must have at least one string!");
36
Steve Naroff4f88b312007-03-13 22:37:02 +000037 StringLiteralParser Literal(StringToks, NumStringToks, PP, Context.Target);
38 if (Literal.hadError)
39 return ExprResult(true);
Chris Lattner5b183d82006-11-10 05:03:26 +000040
Chris Lattner23b7eb62007-06-15 23:05:46 +000041 llvm::SmallVector<SourceLocation, 4> StringTokLocs;
Chris Lattner5b183d82006-11-10 05:03:26 +000042 for (unsigned i = 0; i != NumStringToks; ++i)
43 StringTokLocs.push_back(StringToks[i].getLocation());
Steve Narofff1e53692007-03-23 22:27:02 +000044
45 // FIXME: handle wchar_t
Steve Naroffe5aa9be2007-04-05 22:36:20 +000046 QualType t = Context.getPointerType(Context.CharTy);
Steve Narofff1e53692007-03-23 22:27:02 +000047
Chris Lattner5b183d82006-11-10 05:03:26 +000048 // Pass &StringTokLocs[0], StringTokLocs.size() to factory!
Steve Naroff4f88b312007-03-13 22:37:02 +000049 return new StringLiteral(Literal.GetString(), Literal.GetStringLength(),
Steve Naroff53f07dc2007-05-17 21:49:33 +000050 Literal.AnyWide, t, StringToks[0].getLocation(),
51 StringToks[NumStringToks-1].getLocation());
Chris Lattner5b183d82006-11-10 05:03:26 +000052}
53
Chris Lattnere168f762006-11-10 05:29:30 +000054
Chris Lattnerac18be92006-11-20 06:49:47 +000055/// ParseIdentifierExpr - The parser read an identifier in expression context,
56/// validate it per-C99 6.5.1. HasTrailingLParen indicates whether this
57/// identifier is used in an function call context.
58Sema::ExprResult Sema::ParseIdentifierExpr(Scope *S, SourceLocation Loc,
59 IdentifierInfo &II,
60 bool HasTrailingLParen) {
Chris Lattner17ed4872006-11-20 04:58:19 +000061 // Could be enum-constant or decl.
Chris Lattner9561a0b2007-01-28 08:20:04 +000062 Decl *D = LookupScopedDecl(&II, Decl::IDNS_Ordinary, Loc, S);
Chris Lattner17ed4872006-11-20 04:58:19 +000063 if (D == 0) {
Bill Wendling4073ed52007-02-13 01:51:42 +000064 // Otherwise, this could be an implicitly declared function reference (legal
Chris Lattner9561a0b2007-01-28 08:20:04 +000065 // in C90, extension in C99).
Chris Lattnerac18be92006-11-20 06:49:47 +000066 if (HasTrailingLParen &&
67 // Not in C++.
Steve Narofff1e53692007-03-23 22:27:02 +000068 !getLangOptions().CPlusPlus)
Chris Lattnerac18be92006-11-20 06:49:47 +000069 D = ImplicitlyDefineFunction(Loc, II, S);
Steve Naroff92e30f82007-04-02 22:35:25 +000070 else {
Chris Lattnerac18be92006-11-20 06:49:47 +000071 // If this name wasn't predeclared and if this is not a function call,
72 // diagnose the problem.
Steve Narofff1e53692007-03-23 22:27:02 +000073 return Diag(Loc, diag::err_undeclared_var_use, II.getName());
Steve Naroff92e30f82007-04-02 22:35:25 +000074 }
Chris Lattner17ed4872006-11-20 04:58:19 +000075 }
76
Steve Naroff46ba1eb2007-04-03 23:13:13 +000077 if (ValueDecl *VD = dyn_cast<ValueDecl>(D))
Steve Naroff509fe022007-05-17 01:16:00 +000078 return new DeclRefExpr(VD, VD->getType(), Loc);
Steve Naroff46ba1eb2007-04-03 23:13:13 +000079 if (isa<TypedefDecl>(D))
Steve Narofff1e53692007-03-23 22:27:02 +000080 return Diag(Loc, diag::err_unexpected_typedef, II.getName());
81
82 assert(0 && "Invalid decl");
Chris Lattner17ed4872006-11-20 04:58:19 +000083}
Chris Lattnere168f762006-11-10 05:29:30 +000084
Chris Lattner17ed4872006-11-20 04:58:19 +000085Sema::ExprResult Sema::ParseSimplePrimaryExpr(SourceLocation Loc,
86 tok::TokenKind Kind) {
Chris Lattnere168f762006-11-10 05:29:30 +000087 switch (Kind) {
88 default:
89 assert(0 && "Unknown simple primary expr!");
Steve Naroffe4718892007-04-27 18:30:00 +000090 // TODO: MOVE this to be some other callback.
Chris Lattnere168f762006-11-10 05:29:30 +000091 case tok::kw___func__: // primary-expression: __func__ [C99 6.4.2.2]
92 case tok::kw___FUNCTION__: // primary-expression: __FUNCTION__ [GNU]
93 case tok::kw___PRETTY_FUNCTION__: // primary-expression: __P..Y_F..N__ [GNU]
Chris Lattner17ed4872006-11-20 04:58:19 +000094 return 0;
Chris Lattnere168f762006-11-10 05:29:30 +000095 }
96}
97
Steve Naroffae4143e2007-04-26 20:39:23 +000098Sema::ExprResult Sema::ParseCharacterConstant(const LexerToken &Tok) {
Chris Lattner23b7eb62007-06-15 23:05:46 +000099 llvm::SmallString<16> CharBuffer;
Steve Naroffae4143e2007-04-26 20:39:23 +0000100 CharBuffer.resize(Tok.getLength());
101 const char *ThisTokBegin = &CharBuffer[0];
102 unsigned ActualLength = PP.getSpelling(Tok, ThisTokBegin);
103
104 CharLiteralParser Literal(ThisTokBegin, ThisTokBegin+ActualLength,
105 Tok.getLocation(), PP);
106 if (Literal.hadError())
107 return ExprResult(true);
Steve Naroff509fe022007-05-17 01:16:00 +0000108 return new CharacterLiteral(Literal.getValue(), Context.IntTy,
109 Tok.getLocation());
Steve Naroffae4143e2007-04-26 20:39:23 +0000110}
111
Steve Naroff8160ea22007-03-06 01:09:46 +0000112Action::ExprResult Sema::ParseNumericConstant(const LexerToken &Tok) {
Steve Narofff2fb89e2007-03-13 20:29:44 +0000113 // fast path for a single digit (which is quite common). A single digit
114 // cannot have a trigraph, escaped newline, radix prefix, or type suffix.
115 if (Tok.getLength() == 1) {
116 const char *t = PP.getSourceManager().getCharacterData(Tok.getLocation());
Chris Lattner67ca9252007-05-21 01:08:44 +0000117
Chris Lattner4481b422007-07-14 01:29:45 +0000118 unsigned IntSize = Context.getTypeSize(Context.IntTy, Tok.getLocation());
Chris Lattner23b7eb62007-06-15 23:05:46 +0000119 return ExprResult(new IntegerLiteral(llvm::APInt(IntSize, *t-'0'),
120 Context.IntTy,
Steve Naroff509fe022007-05-17 01:16:00 +0000121 Tok.getLocation()));
Steve Narofff2fb89e2007-03-13 20:29:44 +0000122 }
Chris Lattner23b7eb62007-06-15 23:05:46 +0000123 llvm::SmallString<512> IntegerBuffer;
Steve Naroff8160ea22007-03-06 01:09:46 +0000124 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.
Chris Lattner23b7eb62007-06-15 23:05:46 +0000138 llvm::APInt ResultVal(Context.Target.getIntMaxTWidth(Tok.getLocation()), 0);
Chris Lattner67ca9252007-05-21 01:08:44 +0000139
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;
Chris Lattner4481b422007-07-14 01:29:45 +0000144 assert(Context.getTypeSize(t, Tok.getLocation()) ==
Chris Lattner67ca9252007-05-21 01:08:44 +0000145 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?
Chris Lattner4481b422007-07-14 01:29:45 +0000156 unsigned IntSize = Context.getTypeSize(Context.IntTy,Tok.getLocation());
Chris Lattner67ca9252007-05-21 01:08:44 +0000157 // 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) {
Chris Lattner4481b422007-07-14 01:29:45 +0000172 unsigned LongSize = Context.getTypeSize(Context.LongTy,
173 Tok.getLocation());
Chris Lattner67ca9252007-05-21 01:08:44 +0000174
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 =
Chris Lattner4481b422007-07-14 01:29:45 +0000190 Context.getTypeSize(Context.LongLongTy, Tok.getLocation());
Chris Lattner67ca9252007-05-21 01:08:44 +0000191
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 Naroff97b9e912007-07-09 23:53:58 +0000212 // FIXME: handle float values > 32 (including compute the real type...).
213 return new FloatingLiteral(Literal.GetFloatValue(), Context.FloatTy,
214 Tok.getLocation());
Steve Naroff09ef4742007-03-09 23:16:33 +0000215 }
Steve Narofff2fb89e2007-03-13 20:29:44 +0000216 return ExprResult(true);
Chris Lattnere168f762006-11-10 05:29:30 +0000217}
218
219Action::ExprResult Sema::ParseParenExpr(SourceLocation L, SourceLocation R,
220 ExprTy *Val) {
Steve Naroffae4143e2007-04-26 20:39:23 +0000221 Expr *e = (Expr *)Val;
222 assert((e != 0) && "ParseParenExpr() missing expr");
Steve Naroff53f07dc2007-05-17 21:49:33 +0000223 return new ParenExpr(L, R, e);
Chris Lattnere168f762006-11-10 05:29:30 +0000224}
225
Steve Naroff71b59a92007-06-04 22:22:31 +0000226/// The UsualUnaryConversions() function is *not* called by this routine.
Steve Naroffa78fe7e2007-05-16 19:47:19 +0000227/// See C99 6.3.2.1p[2-4] for more details.
Steve Naroff043d45d2007-05-15 02:32:35 +0000228QualType Sema::CheckSizeOfAlignOfOperand(QualType exprType,
229 SourceLocation OpLoc, bool isSizeof) {
230 // C99 6.5.3.4p1:
231 if (isa<FunctionType>(exprType) && isSizeof)
232 // alignof(function) is allowed.
233 Diag(OpLoc, diag::ext_sizeof_function_type);
234 else if (exprType->isVoidType())
235 Diag(OpLoc, diag::ext_sizeof_void_type, isSizeof ? "sizeof" : "__alignof");
236 else if (exprType->isIncompleteType()) {
Steve Naroff043d45d2007-05-15 02:32:35 +0000237 Diag(OpLoc, isSizeof ? diag::err_sizeof_incomplete_type :
Chris Lattner3dc3d772007-05-16 18:07:12 +0000238 diag::err_alignof_incomplete_type,
239 exprType.getAsString());
Steve Naroff043d45d2007-05-15 02:32:35 +0000240 return QualType(); // error
241 }
242 // C99 6.5.3.4p4: the type (an unsigned integer type) is size_t.
243 return Context.getSizeType();
244}
245
Chris Lattnere168f762006-11-10 05:29:30 +0000246Action::ExprResult Sema::
247ParseSizeOfAlignOfTypeExpr(SourceLocation OpLoc, bool isSizeof,
Steve Naroff509fe022007-05-17 01:16:00 +0000248 SourceLocation LPLoc, TypeTy *Ty,
249 SourceLocation RPLoc) {
Chris Lattner0d8b1a12006-11-20 04:34:45 +0000250 // If error parsing type, ignore.
251 if (Ty == 0) return true;
Chris Lattner6531c102007-01-23 22:29:49 +0000252
253 // Verify that this is a valid expression.
Steve Naroffe5aa9be2007-04-05 22:36:20 +0000254 QualType ArgTy = QualType::getFromOpaquePtr(Ty);
Chris Lattner6531c102007-01-23 22:29:49 +0000255
Steve Naroff043d45d2007-05-15 02:32:35 +0000256 QualType resultType = CheckSizeOfAlignOfOperand(ArgTy, OpLoc, isSizeof);
257
258 if (resultType.isNull())
259 return true;
Steve Naroff509fe022007-05-17 01:16:00 +0000260 return new SizeOfAlignOfTypeExpr(isSizeof, ArgTy, resultType, OpLoc, RPLoc);
Chris Lattnere168f762006-11-10 05:29:30 +0000261}
262
263
264Action::ExprResult Sema::ParsePostfixUnaryOp(SourceLocation OpLoc,
265 tok::TokenKind Kind,
266 ExprTy *Input) {
267 UnaryOperator::Opcode Opc;
268 switch (Kind) {
269 default: assert(0 && "Unknown unary op!");
270 case tok::plusplus: Opc = UnaryOperator::PostInc; break;
271 case tok::minusminus: Opc = UnaryOperator::PostDec; break;
272 }
Steve Naroff71ce2e02007-05-18 22:53:50 +0000273 QualType result = CheckIncrementDecrementOperand((Expr *)Input, OpLoc);
Steve Naroff35d85152007-05-07 00:24:15 +0000274 if (result.isNull())
275 return true;
Steve Naroff509fe022007-05-17 01:16:00 +0000276 return new UnaryOperator((Expr *)Input, Opc, result, OpLoc);
Chris Lattnere168f762006-11-10 05:29:30 +0000277}
278
279Action::ExprResult Sema::
280ParseArraySubscriptExpr(ExprTy *Base, SourceLocation LLoc,
281 ExprTy *Idx, SourceLocation RLoc) {
Chris Lattner5981db42007-07-15 23:59:53 +0000282 Expr *LHSExp = static_cast<Expr*>(Base), *RHSExp = static_cast<Expr*>(Idx);
283 QualType LHSTy = LHSExp->getType(), RHSTy = RHSExp->getType();
284
285 assert(!LHSTy.isNull() && !RHSTy.isNull() && "missing types");
Steve Narofff1e53692007-03-23 22:27:02 +0000286
Chris Lattner5981db42007-07-15 23:59:53 +0000287 QualType canonT1 = DefaultFunctionArrayConversion(LHSExp).getCanonicalType();
288 QualType canonT2 = DefaultFunctionArrayConversion(RHSExp).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 Naroffb3096442007-06-09 03:47:53 +0000295 Expr *baseExpr, *indexExpr;
Steve Naroffe5aa9be2007-04-05 22:36:20 +0000296 QualType baseType, indexType;
Steve Naroffb29cdd52007-07-10 18:23:31 +0000297 if (isa<PointerType>(canonT1) || isa<VectorType>(canonT1)) {
Steve Naroffd50c88e2007-04-05 21:15:20 +0000298 baseType = canonT1;
299 indexType = canonT2;
Chris Lattner5981db42007-07-15 23:59:53 +0000300 baseExpr = LHSExp;
301 indexExpr = RHSExp;
Steve Naroffb3096442007-06-09 03:47:53 +0000302 } else if (isa<PointerType>(canonT2)) { // uncommon
Steve Naroffd50c88e2007-04-05 21:15:20 +0000303 baseType = canonT2;
304 indexType = canonT1;
Chris Lattner5981db42007-07-15 23:59:53 +0000305 baseExpr = RHSExp;
306 indexExpr = LHSExp;
Steve Naroffb3096442007-06-09 03:47:53 +0000307 } else {
Chris Lattner5981db42007-07-15 23:59:53 +0000308 return Diag(LHSExp->getLocStart(), diag::err_typecheck_subscript_value,
309 RHSExp->getSourceRange());
Steve Naroffb3096442007-06-09 03:47:53 +0000310 }
Steve Naroffc1aadb12007-03-28 21:49:40 +0000311 // C99 6.5.2.1p1
Steve Naroffb3096442007-06-09 03:47:53 +0000312 if (!indexType->isIntegerType()) {
313 return Diag(indexExpr->getLocStart(), diag::err_typecheck_subscript,
314 indexExpr->getSourceRange());
315 }
Steve Naroffb29cdd52007-07-10 18:23:31 +0000316 QualType resultType;
317 if (PointerType *ary = dyn_cast<PointerType>(baseType)) {
318 // FIXME: need to deal with const...
319 resultType = ary->getPointeeType();
320 // in practice, the following check catches trying to index a pointer
321 // to a function (e.g. void (*)(int)). Functions are not objects in c99.
322 if (!resultType->isObjectType()) {
323 return Diag(baseExpr->getLocStart(),
324 diag::err_typecheck_subscript_not_object,
325 baseType.getAsString(), baseExpr->getSourceRange());
326 }
327 } else if (VectorType *vec = dyn_cast<VectorType>(baseType))
328 resultType = vec->getElementType();
329
Steve Naroff509fe022007-05-17 01:16:00 +0000330 return new ArraySubscriptExpr((Expr*)Base, (Expr*)Idx, resultType, RLoc);
Chris Lattnere168f762006-11-10 05:29:30 +0000331}
332
333Action::ExprResult Sema::
334ParseMemberReferenceExpr(ExprTy *Base, SourceLocation OpLoc,
335 tok::TokenKind OpKind, SourceLocation MemberLoc,
336 IdentifierInfo &Member) {
Steve Naroffe5aa9be2007-04-05 22:36:20 +0000337 QualType qualifiedType = ((Expr *)Base)->getType();
Steve Naroffca8f7122007-04-01 01:41:35 +0000338
339 assert(!qualifiedType.isNull() && "no type for member expression");
340
Steve Naroffe5aa9be2007-04-05 22:36:20 +0000341 QualType canonType = qualifiedType.getCanonicalType();
Steve Narofff1e53692007-03-23 22:27:02 +0000342
343 if (OpKind == tok::arrow) {
Steve Naroffca8f7122007-04-01 01:41:35 +0000344 if (PointerType *PT = dyn_cast<PointerType>(canonType)) {
345 qualifiedType = PT->getPointeeType();
Steve Naroffd50c88e2007-04-05 21:15:20 +0000346 canonType = qualifiedType.getCanonicalType();
Steve Naroffca8f7122007-04-01 01:41:35 +0000347 } else
Steve Narofff1e53692007-03-23 22:27:02 +0000348 return Diag(OpLoc, diag::err_typecheck_member_reference_arrow);
349 }
Steve Naroff92e30f82007-04-02 22:35:25 +0000350 if (!isa<RecordType>(canonType))
351 return Diag(OpLoc, diag::err_typecheck_member_reference_structUnion);
352
353 // get the struct/union definition from the type.
354 RecordDecl *RD = cast<RecordType>(canonType)->getDecl();
Steve Naroffcc321422007-03-26 23:09:51 +0000355
Steve Naroff92e30f82007-04-02 22:35:25 +0000356 if (canonType->isIncompleteType())
357 return Diag(OpLoc, diag::err_typecheck_incomplete_tag, RD->getName());
Steve Naroffcc321422007-03-26 23:09:51 +0000358
Steve Naroff92e30f82007-04-02 22:35:25 +0000359 FieldDecl *MemberDecl = RD->getMember(&Member);
360 if (!MemberDecl)
361 return Diag(OpLoc, diag::err_typecheck_no_member, Member.getName());
362
Steve Naroff9358c712007-05-27 23:58:33 +0000363 return new MemberExpr((Expr*)Base, OpKind == tok::arrow,
364 MemberDecl, MemberLoc);
Chris Lattnere168f762006-11-10 05:29:30 +0000365}
366
367/// ParseCallExpr - Handle a call to Fn with the specified array of arguments.
368/// This provides the location of the left/right parens and a list of comma
369/// locations.
370Action::ExprResult Sema::
371ParseCallExpr(ExprTy *Fn, SourceLocation LParenLoc,
Steve Naroffb8c289d2007-05-08 22:18:00 +0000372 ExprTy **Args, unsigned NumArgsInCall,
Chris Lattnere168f762006-11-10 05:29:30 +0000373 SourceLocation *CommaLocs, SourceLocation RParenLoc) {
Steve Naroff8563f652007-05-28 19:25:56 +0000374 Expr *funcExpr = (Expr *)Fn;
Steve Naroff8563f652007-05-28 19:25:56 +0000375 assert(funcExpr && "no function call expression");
376
Steve Naroff7a5af782007-07-13 16:58:59 +0000377 QualType qType = UsualUnaryConversions(funcExpr);
Steve Naroffae4143e2007-04-26 20:39:23 +0000378 assert(!qType.isNull() && "no type for function call expression");
379
Chris Lattner3d01e4e2007-06-06 05:14:05 +0000380 // C99 6.5.2.2p1 - "The expression that denotes the called function shall have
381 // type pointer to function".
382 const PointerType *PT = dyn_cast<PointerType>(qType);
383 if (PT == 0) PT = dyn_cast<PointerType>(qType.getCanonicalType());
384
385 if (PT == 0)
386 return Diag(funcExpr->getLocStart(), diag::err_typecheck_call_not_function,
387 SourceRange(funcExpr->getLocStart(), RParenLoc));
Chris Lattner3343f812007-06-06 05:05:41 +0000388
389 const FunctionType *funcT = dyn_cast<FunctionType>(PT->getPointeeType());
Chris Lattner3d01e4e2007-06-06 05:14:05 +0000390 if (funcT == 0)
391 funcT = dyn_cast<FunctionType>(PT->getPointeeType().getCanonicalType());
392
393 if (funcT == 0)
394 return Diag(funcExpr->getLocStart(), diag::err_typecheck_call_not_function,
395 SourceRange(funcExpr->getLocStart(), RParenLoc));
Steve Naroffb8c289d2007-05-08 22:18:00 +0000396
Steve Naroff17f76e02007-05-03 21:03:48 +0000397 // If a prototype isn't declared, the parser implicitly defines a func decl
398 QualType resultType = funcT->getResultType();
399
400 if (const FunctionTypeProto *proto = dyn_cast<FunctionTypeProto>(funcT)) {
401 // C99 6.5.2.2p7 - the arguments are implicitly converted, as if by
402 // assignment, to the types of the corresponding parameter, ...
403
404 unsigned NumArgsInProto = proto->getNumArgs();
Steve Naroffb8c289d2007-05-08 22:18:00 +0000405 unsigned NumArgsToCheck = NumArgsInCall;
Steve Naroff17f76e02007-05-03 21:03:48 +0000406
Steve Naroffb8c289d2007-05-08 22:18:00 +0000407 if (NumArgsInCall < NumArgsInProto)
Steve Naroff56faab22007-05-30 04:20:12 +0000408 Diag(RParenLoc, diag::err_typecheck_call_too_few_args,
Steve Naroffeb9da942007-05-30 00:06:37 +0000409 funcExpr->getSourceRange());
Steve Naroffb8c289d2007-05-08 22:18:00 +0000410 else if (NumArgsInCall > NumArgsInProto) {
Steve Naroff8563f652007-05-28 19:25:56 +0000411 if (!proto->isVariadic()) {
Steve Naroff56faab22007-05-30 04:20:12 +0000412 Diag(((Expr **)Args)[NumArgsInProto+1]->getLocStart(),
413 diag::err_typecheck_call_too_many_args, funcExpr->getSourceRange(),
414 ((Expr **)Args)[NumArgsInProto+1]->getSourceRange());
Steve Naroff8563f652007-05-28 19:25:56 +0000415 }
Steve Naroffb8c289d2007-05-08 22:18:00 +0000416 NumArgsToCheck = NumArgsInProto;
Steve Naroff17f76e02007-05-03 21:03:48 +0000417 }
418 // Continue to check argument types (even if we have too few/many args).
Steve Naroffb8c289d2007-05-08 22:18:00 +0000419 for (unsigned i = 0; i < NumArgsToCheck; i++) {
Steve Naroff8563f652007-05-28 19:25:56 +0000420 Expr *argExpr = ((Expr **)Args)[i];
Steve Naroff8563f652007-05-28 19:25:56 +0000421 assert(argExpr && "ParseCallExpr(): missing argument expression");
422
Steve Naroff17f76e02007-05-03 21:03:48 +0000423 QualType lhsType = proto->getArgType(i);
Steve Naroff8563f652007-05-28 19:25:56 +0000424 QualType rhsType = argExpr->getType();
Steve Naroff17f76e02007-05-03 21:03:48 +0000425
426 if (lhsType == rhsType) // common case, fast path...
427 continue;
Steve Naroff98cf3e92007-06-06 18:38:38 +0000428
Steve Naroffb8ea4fb2007-07-13 23:32:42 +0000429 AssignmentCheckResult result = CheckSingleAssignmentConstraints(lhsType,
430 argExpr);
Steve Naroff6f49f5d2007-05-29 14:23:36 +0000431 SourceLocation l = argExpr->getLocStart();
Steve Naroff17f76e02007-05-03 21:03:48 +0000432
433 // decode the result (notice that AST's are still created for extensions).
Steve Naroff17f76e02007-05-03 21:03:48 +0000434 switch (result) {
435 case Compatible:
436 break;
437 case PointerFromInt:
Steve Naroff218bc2b2007-05-04 21:54:46 +0000438 // check for null pointer constant (C99 6.3.2.3p3)
Chris Lattner0e9d6222007-07-15 23:26:56 +0000439 if (!argExpr->isNullPointerConstant(Context)) {
Steve Naroff56faab22007-05-30 04:20:12 +0000440 Diag(l, diag::ext_typecheck_passing_pointer_int,
441 lhsType.getAsString(), rhsType.getAsString(),
Steve Naroffeb9da942007-05-30 00:06:37 +0000442 funcExpr->getSourceRange(), argExpr->getSourceRange());
443 }
Steve Naroff17f76e02007-05-03 21:03:48 +0000444 break;
445 case IntFromPointer:
Steve Naroff56faab22007-05-30 04:20:12 +0000446 Diag(l, diag::ext_typecheck_passing_pointer_int,
447 lhsType.getAsString(), rhsType.getAsString(),
Steve Naroffeb9da942007-05-30 00:06:37 +0000448 funcExpr->getSourceRange(), argExpr->getSourceRange());
Steve Naroff17f76e02007-05-03 21:03:48 +0000449 break;
450 case IncompatiblePointer:
Steve Naroff8563f652007-05-28 19:25:56 +0000451 Diag(l, diag::ext_typecheck_passing_incompatible_pointer,
Steve Naroffeb9da942007-05-30 00:06:37 +0000452 rhsType.getAsString(), lhsType.getAsString(),
453 funcExpr->getSourceRange(), argExpr->getSourceRange());
Steve Naroff17f76e02007-05-03 21:03:48 +0000454 break;
Steve Naroff1f4d7272007-05-11 04:00:31 +0000455 case CompatiblePointerDiscardsQualifiers:
Steve Naroffeb9da942007-05-30 00:06:37 +0000456 Diag(l, diag::ext_typecheck_passing_discards_qualifiers,
457 rhsType.getAsString(), lhsType.getAsString(),
458 funcExpr->getSourceRange(), argExpr->getSourceRange());
Steve Naroff1f4d7272007-05-11 04:00:31 +0000459 break;
Steve Naroff17f76e02007-05-03 21:03:48 +0000460 case Incompatible:
Steve Naroff8563f652007-05-28 19:25:56 +0000461 return Diag(l, diag::err_typecheck_passing_incompatible,
462 rhsType.getAsString(), lhsType.getAsString(),
463 funcExpr->getSourceRange(), argExpr->getSourceRange());
Steve Naroff17f76e02007-05-03 21:03:48 +0000464 }
465 }
Steve Naroffb8c289d2007-05-08 22:18:00 +0000466 // Even if the types checked, bail if we had the wrong number of arguments.
467 if ((NumArgsInCall != NumArgsInProto) && !proto->isVariadic())
468 return true;
Steve Naroffae4143e2007-04-26 20:39:23 +0000469 }
Steve Naroff509fe022007-05-17 01:16:00 +0000470 return new CallExpr((Expr*)Fn, (Expr**)Args, NumArgsInCall, resultType,
471 RParenLoc);
Chris Lattnere168f762006-11-10 05:29:30 +0000472}
473
474Action::ExprResult Sema::
475ParseCastExpr(SourceLocation LParenLoc, TypeTy *Ty,
476 SourceLocation RParenLoc, ExprTy *Op) {
Chris Lattner0d8b1a12006-11-20 04:34:45 +0000477 // If error parsing type, ignore.
Steve Naroffae4143e2007-04-26 20:39:23 +0000478 assert((Ty != 0) && "ParseCastExpr(): missing type");
Chris Lattner1d411a82007-06-05 20:52:21 +0000479 // FIXME: Sema for cast is completely missing.
Steve Naroff509fe022007-05-17 01:16:00 +0000480 return new CastExpr(QualType::getFromOpaquePtr(Ty), (Expr*)Op, LParenLoc);
Chris Lattnere168f762006-11-10 05:29:30 +0000481}
482
Steve Narofff8a28c52007-05-15 20:29:32 +0000483inline QualType Sema::CheckConditionalOperands( // C99 6.5.15
Steve Naroff7a5af782007-07-13 16:58:59 +0000484 Expr *&cond, Expr *&lex, Expr *&rex, SourceLocation questionLoc) {
Steve Naroffb66fb742007-07-13 17:39:21 +0000485 QualType condT = UsualUnaryConversions(cond);
486 QualType lexT = UsualUnaryConversions(lex);
487 QualType rexT = UsualUnaryConversions(rex);
Steve Naroffa78fe7e2007-05-16 19:47:19 +0000488
489 // first, check the condition.
Steve Naroff7a5af782007-07-13 16:58:59 +0000490 if (!condT->isScalarType()) { // C99 6.5.15p2
491 Diag(cond->getLocStart(), diag::err_typecheck_cond_expect_scalar,
492 condT.getAsString());
Steve Naroffa78fe7e2007-05-16 19:47:19 +0000493 return QualType();
494 }
495 // now check the two expressions.
Steve Naroff7a5af782007-07-13 16:58:59 +0000496 if (lexT->isArithmeticType() && rexT->isArithmeticType()) // C99 6.5.15p3,5
Steve Naroffb8ea4fb2007-07-13 23:32:42 +0000497 return UsualArithmeticConversions(lex, rex, lexT, rexT);
Steve Naroffa78fe7e2007-05-16 19:47:19 +0000498
Steve Naroff7a5af782007-07-13 16:58:59 +0000499 if ((lexT->isStructureType() && rexT->isStructureType()) || // C99 6.5.15p3
500 (lexT->isUnionType() && rexT->isUnionType())) {
501 TagType *lTag = cast<TagType>(lexT.getCanonicalType());
502 TagType *rTag = cast<TagType>(rexT.getCanonicalType());
Steve Naroffa78fe7e2007-05-16 19:47:19 +0000503
504 if (lTag->getDecl()->getIdentifier() == rTag->getDecl()->getIdentifier())
Steve Naroff7a5af782007-07-13 16:58:59 +0000505 return lexT;
Steve Naroffa78fe7e2007-05-16 19:47:19 +0000506 else {
507 Diag(questionLoc, diag::err_typecheck_cond_incompatible_operands,
Steve Naroff7a5af782007-07-13 16:58:59 +0000508 lexT.getAsString(), rexT.getAsString(),
509 lex->getSourceRange(), rex->getSourceRange());
Steve Naroffa78fe7e2007-05-16 19:47:19 +0000510 return QualType();
511 }
512 }
Chris Lattner0e9d6222007-07-15 23:26:56 +0000513 // C99 6.5.15p3
514 if (lexT->isPointerType() && rex->isNullPointerConstant(Context))
Steve Naroff7a5af782007-07-13 16:58:59 +0000515 return lexT;
Chris Lattner0e9d6222007-07-15 23:26:56 +0000516 if (rexT->isPointerType() && lex->isNullPointerConstant(Context))
Steve Naroff7a5af782007-07-13 16:58:59 +0000517 return rexT;
Steve Naroff30d1fbc2007-05-20 19:46:53 +0000518
Steve Naroff7a5af782007-07-13 16:58:59 +0000519 if (lexT->isPointerType() && rexT->isPointerType()) { // C99 6.5.15p3,6
Steve Naroffa78fe7e2007-05-16 19:47:19 +0000520 QualType lhptee, rhptee;
521
522 // get the "pointed to" type
Steve Naroff7a5af782007-07-13 16:58:59 +0000523 lhptee = cast<PointerType>(lexT.getCanonicalType())->getPointeeType();
524 rhptee = cast<PointerType>(rexT.getCanonicalType())->getPointeeType();
Steve Naroffa78fe7e2007-05-16 19:47:19 +0000525
526 // ignore qualifiers on void (C99 6.5.15p3, clause 6)
527 if (lhptee.getUnqualifiedType()->isVoidType() &&
528 (rhptee->isObjectType() || rhptee->isIncompleteType()))
Steve Naroff7a5af782007-07-13 16:58:59 +0000529 return lexT;
Steve Naroffa78fe7e2007-05-16 19:47:19 +0000530 if (rhptee.getUnqualifiedType()->isVoidType() &&
531 (lhptee->isObjectType() || lhptee->isIncompleteType()))
Steve Naroff7a5af782007-07-13 16:58:59 +0000532 return rexT;
Steve Naroffa78fe7e2007-05-16 19:47:19 +0000533
534 // FIXME: C99 6.5.15p6: If both operands are pointers to compatible types
535 // *or* to differently qualified versions of compatible types, the result
536 // type is a pointer to an appropriately qualified version of the
537 // *composite* type.
538 if (!Type::typesAreCompatible(lhptee.getUnqualifiedType(),
539 rhptee.getUnqualifiedType())) {
540 Diag(questionLoc, diag::ext_typecheck_cond_incompatible_pointers,
Steve Naroff7a5af782007-07-13 16:58:59 +0000541 lexT.getAsString(), rexT.getAsString(),
542 lex->getSourceRange(), rex->getSourceRange());
543 return lexT; // FIXME: this is an _ext - is this return o.k?
Steve Naroffa78fe7e2007-05-16 19:47:19 +0000544 }
545 }
Steve Naroff7a5af782007-07-13 16:58:59 +0000546 if (lexT->isVoidType() && rexT->isVoidType()) // C99 6.5.15p3
547 return lexT;
Steve Naroffa78fe7e2007-05-16 19:47:19 +0000548
549 Diag(questionLoc, diag::err_typecheck_cond_incompatible_operands,
Steve Naroff7a5af782007-07-13 16:58:59 +0000550 lexT.getAsString(), rexT.getAsString(),
551 lex->getSourceRange(), rex->getSourceRange());
Steve Naroffa78fe7e2007-05-16 19:47:19 +0000552 return QualType();
Steve Narofff8a28c52007-05-15 20:29:32 +0000553}
554
Chris Lattnere168f762006-11-10 05:29:30 +0000555/// ParseConditionalOp - Parse a ?: operation. Note that 'LHS' may be null
556/// in the case of a the GNU conditional expr extension.
557Action::ExprResult Sema::ParseConditionalOp(SourceLocation QuestionLoc,
558 SourceLocation ColonLoc,
559 ExprTy *Cond, ExprTy *LHS,
560 ExprTy *RHS) {
Steve Naroff7a5af782007-07-13 16:58:59 +0000561 QualType result = CheckConditionalOperands((Expr *&)Cond, (Expr *&)LHS,
562 (Expr *&)RHS, QuestionLoc);
Steve Narofff8a28c52007-05-15 20:29:32 +0000563 if (result.isNull())
564 return true;
565 return new ConditionalOperator((Expr*)Cond, (Expr*)LHS, (Expr*)RHS, result);
Chris Lattnere168f762006-11-10 05:29:30 +0000566}
567
Steve Naroff81569d22007-07-15 02:02:06 +0000568// promoteExprToType - a helper function to ensure we create exactly one
569// ImplicitCastExpr. As a convenience (to the caller), we return the type.
570static QualType promoteExprToType(Expr *&expr, QualType type) {
571 if (ImplicitCastExpr *impCast = dyn_cast<ImplicitCastExpr>(expr))
572 impCast->setType(type);
573 else
574 expr = new ImplicitCastExpr(type, expr);
575 return type;
576}
577
578/// DefaultFunctionArrayConversion (C99 6.3.2.1p3, C99 6.3.2.1p4).
579QualType Sema::DefaultFunctionArrayConversion(Expr *&e) {
580 QualType t = e->getType();
Steve Naroffb8ea4fb2007-07-13 23:32:42 +0000581 assert(!t.isNull() && "DefaultFunctionArrayConversion - missing type");
582
Steve Naroff81569d22007-07-15 02:02:06 +0000583 if (t->isFunctionType())
584 return promoteExprToType(e, Context.getPointerType(t));
Steve Naroffa78fe7e2007-05-16 19:47:19 +0000585 if (const ArrayType *ary = dyn_cast<ArrayType>(t.getCanonicalType()))
Steve Naroff81569d22007-07-15 02:02:06 +0000586 return promoteExprToType(e, Context.getPointerType(ary->getElementType()));
Steve Naroff1926c832007-04-24 00:23:05 +0000587 return t;
588}
589
Steve Naroff71b59a92007-06-04 22:22:31 +0000590/// UsualUnaryConversion - Performs various conversions that are common to most
591/// operators (C99 6.3). The conversions of array and function types are
592/// sometimes surpressed. For example, the array->pointer conversion doesn't
593/// apply if the array is an argument to the sizeof or address (&) operators.
594/// In these instances, this routine should *not* be called.
Steve Naroff7a5af782007-07-13 16:58:59 +0000595QualType Sema::UsualUnaryConversions(Expr *&expr) {
596 QualType t = expr->getType();
Steve Naroff71b59a92007-06-04 22:22:31 +0000597 assert(!t.isNull() && "UsualUnaryConversions - missing type");
598
Steve Naroff81569d22007-07-15 02:02:06 +0000599 if (t->isPromotableIntegerType()) // C99 6.3.1.1p2
600 return promoteExprToType(expr, Context.IntTy);
Steve Naroffb8ea4fb2007-07-13 23:32:42 +0000601 return DefaultFunctionArrayConversion(expr);
Steve Naroff71b59a92007-06-04 22:22:31 +0000602}
603
Steve Naroff1926c832007-04-24 00:23:05 +0000604/// UsualArithmeticConversions - Performs various conversions that are common to
605/// binary operators (C99 6.3.1.8). If both operands aren't arithmetic, this
606/// routine returns the first non-arithmetic type found. The client is
607/// responsible for emitting appropriate error diagnostics.
Steve Naroffb8ea4fb2007-07-13 23:32:42 +0000608QualType Sema::UsualArithmeticConversions(Expr *&lhsExpr, Expr *&rhsExpr,
609 QualType &lhs, QualType &rhs) {
610 lhs = UsualUnaryConversions(lhsExpr);
611 rhs = UsualUnaryConversions(rhsExpr);
Steve Naroff1926c832007-04-24 00:23:05 +0000612
Chris Lattner0c46c5d2007-06-02 23:53:17 +0000613 // If both types are identical, no conversion is needed.
614 if (lhs == rhs)
615 return lhs;
616
617 // If either side is a non-arithmetic type (e.g. a pointer), we are done.
618 // The caller can deal with this (e.g. pointer + int).
Steve Naroffb01bbe32007-04-25 01:22:31 +0000619 if (!lhs->isArithmeticType())
620 return lhs;
Steve Narofff633d092007-04-25 19:01:39 +0000621 if (!rhs->isArithmeticType())
Steve Naroffb01bbe32007-04-25 01:22:31 +0000622 return rhs;
Steve Naroff1926c832007-04-24 00:23:05 +0000623
Chris Lattner0c46c5d2007-06-02 23:53:17 +0000624 // At this point, we have two different arithmetic types.
Steve Naroffb01bbe32007-04-25 01:22:31 +0000625
626 // Handle complex types first (C99 6.3.1.8p1).
627 if (lhs->isComplexType() || rhs->isComplexType()) {
628 // if we have an integer operand, the result is the complex type.
Steve Naroff81569d22007-07-15 02:02:06 +0000629 if (rhs->isIntegerType()) // convert the rhs to the lhs complex type.
630 return promoteExprToType(rhsExpr, lhs);
Steve Naroffb01bbe32007-04-25 01:22:31 +0000631
Steve Naroff81569d22007-07-15 02:02:06 +0000632 if (lhs->isIntegerType()) // convert the lhs to the rhs complex type.
633 return promoteExprToType(lhsExpr, rhs);
634
635 // Two complex types. Convert the smaller operand to the bigger result.
636 if (Context.maxComplexType(lhs, rhs) == lhs) // convert the rhs
637 return promoteExprToType(rhsExpr, lhs);
638 return promoteExprToType(lhsExpr, rhs); // convert the lhs
Steve Naroffbf223ba2007-04-24 20:56:26 +0000639 }
Steve Naroffb01bbe32007-04-25 01:22:31 +0000640 // Now handle "real" floating types (i.e. float, double, long double).
641 if (lhs->isRealFloatingType() || rhs->isRealFloatingType()) {
642 // if we have an integer operand, the result is the real floating type.
Steve Naroff81569d22007-07-15 02:02:06 +0000643 if (rhs->isIntegerType()) // convert the rhs to the lhs floating point type.
644 return promoteExprToType(rhsExpr, lhs);
Steve Naroffb01bbe32007-04-25 01:22:31 +0000645
Steve Naroff81569d22007-07-15 02:02:06 +0000646 if (lhs->isIntegerType()) // convert the lhs to the rhs floating point type.
647 return promoteExprToType(lhsExpr, rhs);
648
649 // We have two real floating types, float/complex combos were handled above.
650 // Convert the smaller operand to the bigger result.
651 if (Context.maxFloatingType(lhs, rhs) == lhs) // convert the rhs
652 return promoteExprToType(rhsExpr, lhs);
653 return promoteExprToType(lhsExpr, rhs); // convert the lhs
Steve Naroffb01bbe32007-04-25 01:22:31 +0000654 }
Steve Naroff81569d22007-07-15 02:02:06 +0000655 // Finally, we have two differing integer types.
656 if (Context.maxIntegerType(lhs, rhs) == lhs) // convert the rhs
657 return promoteExprToType(rhsExpr, lhs);
658 return promoteExprToType(lhsExpr, rhs); // convert the lhs
Steve Narofff1e53692007-03-23 22:27:02 +0000659}
660
Steve Naroff3f597292007-05-11 22:18:03 +0000661// CheckPointerTypesForAssignment - This is a very tricky routine (despite
662// being closely modeled after the C99 spec:-). The odd characteristic of this
663// routine is it effectively iqnores the qualifiers on the top level pointee.
664// This circumvents the usual type rules specified in 6.2.7p1 & 6.7.5.[1-3].
665// FIXME: add a couple examples in this comment.
Steve Naroff98cf3e92007-06-06 18:38:38 +0000666Sema::AssignmentCheckResult
667Sema::CheckPointerTypesForAssignment(QualType lhsType, QualType rhsType) {
Steve Naroff3f597292007-05-11 22:18:03 +0000668 QualType lhptee, rhptee;
Steve Naroff1f4d7272007-05-11 04:00:31 +0000669
670 // get the "pointed to" type (ignoring qualifiers at the top level)
Steve Naroff3f597292007-05-11 22:18:03 +0000671 lhptee = cast<PointerType>(lhsType.getCanonicalType())->getPointeeType();
672 rhptee = cast<PointerType>(rhsType.getCanonicalType())->getPointeeType();
Steve Naroff1f4d7272007-05-11 04:00:31 +0000673
674 // make sure we operate on the canonical type
Steve Naroff3f597292007-05-11 22:18:03 +0000675 lhptee = lhptee.getCanonicalType();
676 rhptee = rhptee.getCanonicalType();
Steve Naroff1f4d7272007-05-11 04:00:31 +0000677
Steve Naroff98cf3e92007-06-06 18:38:38 +0000678 AssignmentCheckResult r = Compatible;
679
Steve Naroff3f597292007-05-11 22:18:03 +0000680 // C99 6.5.16.1p1: This following citation is common to constraints
681 // 3 & 4 (below). ...and the type *pointed to* by the left has all the
682 // qualifiers of the type *pointed to* by the right;
683 if ((lhptee.getQualifiers() & rhptee.getQualifiers()) !=
684 rhptee.getQualifiers())
685 r = CompatiblePointerDiscardsQualifiers;
686
687 // C99 6.5.16.1p1 (constraint 4): If one operand is a pointer to an object or
688 // incomplete type and the other is a pointer to a qualified or unqualified
689 // version of void...
690 if (lhptee.getUnqualifiedType()->isVoidType() &&
691 (rhptee->isObjectType() || rhptee->isIncompleteType()))
692 ;
693 else if (rhptee.getUnqualifiedType()->isVoidType() &&
694 (lhptee->isObjectType() || lhptee->isIncompleteType()))
695 ;
696 // C99 6.5.16.1p1 (constraint 3): both operands are pointers to qualified or
697 // unqualified versions of compatible types, ...
698 else if (!Type::typesAreCompatible(lhptee.getUnqualifiedType(),
699 rhptee.getUnqualifiedType()))
700 r = IncompatiblePointer; // this "trumps" PointerAssignDiscardsQualifiers
Steve Naroff98cf3e92007-06-06 18:38:38 +0000701 return r;
Steve Naroff1f4d7272007-05-11 04:00:31 +0000702}
703
Steve Naroff98cf3e92007-06-06 18:38:38 +0000704/// CheckAssignmentConstraints (C99 6.5.16) - This routine currently
Steve Naroff17f76e02007-05-03 21:03:48 +0000705/// has code to accommodate several GCC extensions when type checking
706/// pointers. Here are some objectionable examples that GCC considers warnings:
707///
708/// int a, *pint;
709/// short *pshort;
710/// struct foo *pfoo;
711///
712/// pint = pshort; // warning: assignment from incompatible pointer type
713/// a = pint; // warning: assignment makes integer from pointer without a cast
714/// pint = a; // warning: assignment makes pointer from integer without a cast
715/// pint = pfoo; // warning: assignment from incompatible pointer type
716///
717/// As a result, the code for dealing with pointers is more complex than the
718/// C99 spec dictates.
719/// Note: the warning above turn into errors when -pedantic-errors is enabled.
720///
Steve Naroff98cf3e92007-06-06 18:38:38 +0000721Sema::AssignmentCheckResult
722Sema::CheckAssignmentConstraints(QualType lhsType, QualType rhsType) {
Steve Naroff84ff4b42007-07-09 21:31:10 +0000723 if (lhsType->isArithmeticType() && rhsType->isArithmeticType()) {
Steve Naroffe728ba32007-07-10 22:20:04 +0000724 if (lhsType->isVectorType() || rhsType->isVectorType()) {
725 if (lhsType.getCanonicalType() != rhsType.getCanonicalType())
726 return Incompatible;
727 }
Steve Naroff98cf3e92007-06-06 18:38:38 +0000728 return Compatible;
Steve Naroff84ff4b42007-07-09 21:31:10 +0000729 } else if (lhsType->isPointerType()) {
Steve Naroff98cf3e92007-06-06 18:38:38 +0000730 if (rhsType->isIntegerType())
731 return PointerFromInt;
732
Steve Naroff3f597292007-05-11 22:18:03 +0000733 if (rhsType->isPointerType())
Steve Naroff98cf3e92007-06-06 18:38:38 +0000734 return CheckPointerTypesForAssignment(lhsType, rhsType);
Steve Naroff9eb24652007-05-02 21:58:15 +0000735 } else if (rhsType->isPointerType()) {
Steve Naroff98cf3e92007-06-06 18:38:38 +0000736 // C99 6.5.16.1p1: the left operand is _Bool and the right is a pointer.
737 if ((lhsType->isIntegerType()) && (lhsType != Context.BoolTy))
738 return IntFromPointer;
739
Steve Naroff3f597292007-05-11 22:18:03 +0000740 if (lhsType->isPointerType())
Steve Naroff98cf3e92007-06-06 18:38:38 +0000741 return CheckPointerTypesForAssignment(lhsType, rhsType);
Steve Naroff1f4d7272007-05-11 04:00:31 +0000742 } else if (isa<TagType>(lhsType) && isa<TagType>(rhsType)) {
743 if (Type::tagTypesAreCompatible(lhsType, rhsType))
Steve Naroff98cf3e92007-06-06 18:38:38 +0000744 return Compatible;
Bill Wendlingdb4f06e2007-06-02 23:29:59 +0000745 } else if (lhsType->isReferenceType() || rhsType->isReferenceType()) {
746 if (Type::referenceTypesAreCompatible(lhsType, rhsType))
Steve Naroff98cf3e92007-06-06 18:38:38 +0000747 return Compatible;
Bill Wendling216423b2007-05-30 06:30:29 +0000748 }
Steve Naroff98cf3e92007-06-06 18:38:38 +0000749 return Incompatible;
Steve Naroff9eb24652007-05-02 21:58:15 +0000750}
751
Steve Naroffb8ea4fb2007-07-13 23:32:42 +0000752Sema::AssignmentCheckResult
753Sema::CheckSingleAssignmentConstraints(QualType lhsType, Expr *&rExpr) {
754 // This check seems unnatural, however it is necessary to insure the proper
755 // conversion of functions/arrays. If the conversion were done for all
756 // DeclExpr's (created by ParseIdentifierExpr), it would mess up the unary
757 // expressions that surpress this implicit conversion (&, sizeof).
758 QualType rhsType = DefaultFunctionArrayConversion(rExpr);
759
760 return CheckAssignmentConstraints(lhsType, rhsType);
761}
762
763Sema::AssignmentCheckResult
764Sema::CheckCompoundAssignmentConstraints(QualType lhsType, QualType rhsType) {
765 return CheckAssignmentConstraints(lhsType, rhsType);
766}
767
Steve Naroff7a5af782007-07-13 16:58:59 +0000768inline void Sema::InvalidOperands(SourceLocation loc, Expr *&lex, Expr *&rex) {
Steve Naroff6f49f5d2007-05-29 14:23:36 +0000769 Diag(loc, diag::err_typecheck_invalid_operands,
770 lex->getType().getAsString(), rex->getType().getAsString(),
771 lex->getSourceRange(), rex->getSourceRange());
772}
773
Steve Naroff7a5af782007-07-13 16:58:59 +0000774inline QualType Sema::CheckVectorOperands(SourceLocation loc, Expr *&lex,
775 Expr *&rex) {
Steve Naroff84ff4b42007-07-09 21:31:10 +0000776 QualType lhsType = lex->getType(), rhsType = rex->getType();
777
778 // make sure the vector types are identical.
779 if (lhsType == rhsType)
780 return lhsType;
781 // You cannot convert between vector values of different size.
782 Diag(loc, diag::err_typecheck_vector_not_convertable,
783 lex->getType().getAsString(), rex->getType().getAsString(),
784 lex->getSourceRange(), rex->getSourceRange());
785 return QualType();
786}
787
Steve Naroff218bc2b2007-05-04 21:54:46 +0000788inline QualType Sema::CheckMultiplyDivideOperands(
Steve Naroff7a5af782007-07-13 16:58:59 +0000789 Expr *&lex, Expr *&rex, SourceLocation loc)
Steve Naroff5c10d4b2007-04-20 23:42:24 +0000790{
Steve Naroffb8ea4fb2007-07-13 23:32:42 +0000791 QualType lhsType = lex->getType(), rhsType = rex->getType();
792
793 if (lhsType->isVectorType() || rhsType->isVectorType())
Steve Naroff84ff4b42007-07-09 21:31:10 +0000794 return CheckVectorOperands(loc, lex, rex);
Steve Naroff7a5af782007-07-13 16:58:59 +0000795
Steve Naroffb8ea4fb2007-07-13 23:32:42 +0000796 QualType resType = UsualArithmeticConversions(lex, rex, lhsType, rhsType);
Steve Naroff5c10d4b2007-04-20 23:42:24 +0000797
Steve Naroff218bc2b2007-05-04 21:54:46 +0000798 if (resType->isArithmeticType())
799 return resType;
Steve Naroff6f49f5d2007-05-29 14:23:36 +0000800 InvalidOperands(loc, lex, rex);
Steve Naroff218bc2b2007-05-04 21:54:46 +0000801 return QualType();
Steve Naroff26c8ea52007-03-21 21:08:52 +0000802}
803
Steve Naroff218bc2b2007-05-04 21:54:46 +0000804inline QualType Sema::CheckRemainderOperands(
Steve Naroff7a5af782007-07-13 16:58:59 +0000805 Expr *&lex, Expr *&rex, SourceLocation loc)
Steve Naroff218bc2b2007-05-04 21:54:46 +0000806{
Steve Naroffb8ea4fb2007-07-13 23:32:42 +0000807 QualType lhsType = lex->getType(), rhsType = rex->getType();
808
809 QualType resType = UsualArithmeticConversions(lex, rex, lhsType, rhsType);
Steve Naroff218bc2b2007-05-04 21:54:46 +0000810
811 if (resType->isIntegerType())
812 return resType;
Steve Naroff6f49f5d2007-05-29 14:23:36 +0000813 InvalidOperands(loc, lex, rex);
Steve Naroff218bc2b2007-05-04 21:54:46 +0000814 return QualType();
815}
816
817inline QualType Sema::CheckAdditionOperands( // C99 6.5.6
Steve Naroff7a5af782007-07-13 16:58:59 +0000818 Expr *&lex, Expr *&rex, SourceLocation loc)
Steve Naroff1926c832007-04-24 00:23:05 +0000819{
Steve Naroffe4718892007-04-27 18:30:00 +0000820 QualType lhsType = lex->getType(), rhsType = rex->getType();
Steve Naroff84ff4b42007-07-09 21:31:10 +0000821
822 if (lhsType->isVectorType() || rhsType->isVectorType())
Steve Naroff7a5af782007-07-13 16:58:59 +0000823 return CheckVectorOperands(loc, lex, rex);
824
Steve Naroffb8ea4fb2007-07-13 23:32:42 +0000825 QualType resType = UsualArithmeticConversions(lex, rex, lhsType, rhsType);
Steve Naroff4ae0ac62007-07-06 23:09:18 +0000826
Steve Naroffe4718892007-04-27 18:30:00 +0000827 // handle the common case first (both operands are arithmetic).
828 if (resType->isArithmeticType())
Steve Naroff218bc2b2007-05-04 21:54:46 +0000829 return resType;
830
831 if ((lhsType->isPointerType() && rhsType->isIntegerType()) ||
832 (lhsType->isIntegerType() && rhsType->isPointerType()))
833 return resType;
Steve Naroff6f49f5d2007-05-29 14:23:36 +0000834 InvalidOperands(loc, lex, rex);
Steve Naroff218bc2b2007-05-04 21:54:46 +0000835 return QualType();
Steve Naroff26c8ea52007-03-21 21:08:52 +0000836}
837
Steve Naroff218bc2b2007-05-04 21:54:46 +0000838inline QualType Sema::CheckSubtractionOperands( // C99 6.5.6
Steve Naroff7a5af782007-07-13 16:58:59 +0000839 Expr *&lex, Expr *&rex, SourceLocation loc)
Steve Naroff218bc2b2007-05-04 21:54:46 +0000840{
841 QualType lhsType = lex->getType(), rhsType = rex->getType();
Steve Naroff84ff4b42007-07-09 21:31:10 +0000842
843 if (lhsType->isVectorType() || rhsType->isVectorType())
844 return CheckVectorOperands(loc, lex, rex);
Steve Naroffb8ea4fb2007-07-13 23:32:42 +0000845
846 QualType resType = UsualArithmeticConversions(lex, rex, lhsType, rhsType);
Steve Naroff218bc2b2007-05-04 21:54:46 +0000847
848 // handle the common case first (both operands are arithmetic).
849 if (resType->isArithmeticType())
850 return resType;
Chris Lattnerd2b88ab2007-07-13 03:05:23 +0000851 if (lhsType->isPointerType() && rhsType->isIntegerType())
Steve Naroff218bc2b2007-05-04 21:54:46 +0000852 return resType;
Chris Lattnerd2b88ab2007-07-13 03:05:23 +0000853 if (lhsType->isPointerType() && rhsType->isPointerType())
854 return Context.getPointerDiffType();
Steve Naroff6f49f5d2007-05-29 14:23:36 +0000855 InvalidOperands(loc, lex, rex);
Steve Naroff218bc2b2007-05-04 21:54:46 +0000856 return QualType();
857}
858
859inline QualType Sema::CheckShiftOperands( // C99 6.5.7
Steve Naroff7a5af782007-07-13 16:58:59 +0000860 Expr *&lex, Expr *&rex, SourceLocation loc)
Steve Naroff1926c832007-04-24 00:23:05 +0000861{
Chris Lattner1d411a82007-06-05 20:52:21 +0000862 // FIXME: Shifts don't perform usual arithmetic conversions. This is wrong
863 // for int << longlong -> the result type should be int, not long long.
Steve Naroffd6fbee82007-06-13 15:42:33 +0000864 QualType lhsType = lex->getType(), rhsType = rex->getType();
Steve Naroffb8ea4fb2007-07-13 23:32:42 +0000865 QualType resType = UsualArithmeticConversions(lex, rex, lhsType, rhsType);
Steve Naroff1926c832007-04-24 00:23:05 +0000866
Steve Naroff218bc2b2007-05-04 21:54:46 +0000867 if (resType->isIntegerType())
868 return resType;
Steve Naroff6f49f5d2007-05-29 14:23:36 +0000869 InvalidOperands(loc, lex, rex);
Steve Naroff218bc2b2007-05-04 21:54:46 +0000870 return QualType();
Steve Naroff26c8ea52007-03-21 21:08:52 +0000871}
872
Steve Naroff218bc2b2007-05-04 21:54:46 +0000873inline QualType Sema::CheckRelationalOperands( // C99 6.5.8
Steve Naroff7a5af782007-07-13 16:58:59 +0000874 Expr *&lex, Expr *&rex, SourceLocation loc)
Steve Naroff1926c832007-04-24 00:23:05 +0000875{
Steve Naroff7a5af782007-07-13 16:58:59 +0000876 QualType lType = UsualUnaryConversions(lex);
877 QualType rType = UsualUnaryConversions(rex);
Steve Naroff1926c832007-04-24 00:23:05 +0000878
879 if (lType->isRealType() && rType->isRealType())
Steve Naroff218bc2b2007-05-04 21:54:46 +0000880 return Context.IntTy;
Steve Naroffe4718892007-04-27 18:30:00 +0000881
Steve Naroff75c17232007-06-13 21:41:08 +0000882 if (lType->isPointerType()) {
883 if (rType->isPointerType())
884 return Context.IntTy;
885 if (rType->isIntegerType()) {
Chris Lattner0e9d6222007-07-15 23:26:56 +0000886 if (!rex->isNullPointerConstant(Context))
Steve Naroff75c17232007-06-13 21:41:08 +0000887 Diag(loc, diag::ext_typecheck_comparison_of_pointer_integer,
888 lex->getSourceRange(), rex->getSourceRange());
889 return Context.IntTy; // the previous diagnostic is a GCC extension.
890 }
891 } else if (rType->isPointerType()) {
892 if (lType->isIntegerType()) {
Chris Lattner0e9d6222007-07-15 23:26:56 +0000893 if (!lex->isNullPointerConstant(Context))
Steve Naroff75c17232007-06-13 21:41:08 +0000894 Diag(loc, diag::ext_typecheck_comparison_of_pointer_integer,
895 lex->getSourceRange(), rex->getSourceRange());
896 return Context.IntTy; // the previous diagnostic is a GCC extension.
897 }
Steve Naroff043d45d2007-05-15 02:32:35 +0000898 }
Steve Naroff6f49f5d2007-05-29 14:23:36 +0000899 InvalidOperands(loc, lex, rex);
Steve Naroff218bc2b2007-05-04 21:54:46 +0000900 return QualType();
Steve Naroff26c8ea52007-03-21 21:08:52 +0000901}
902
Steve Naroff218bc2b2007-05-04 21:54:46 +0000903inline QualType Sema::CheckEqualityOperands( // C99 6.5.9
Steve Naroff7a5af782007-07-13 16:58:59 +0000904 Expr *&lex, Expr *&rex, SourceLocation loc)
Steve Naroff1926c832007-04-24 00:23:05 +0000905{
Steve Naroff7a5af782007-07-13 16:58:59 +0000906 QualType lType = UsualUnaryConversions(lex);
907 QualType rType = UsualUnaryConversions(rex);
Steve Naroff1926c832007-04-24 00:23:05 +0000908
909 if (lType->isArithmeticType() && rType->isArithmeticType())
Steve Naroff218bc2b2007-05-04 21:54:46 +0000910 return Context.IntTy;
Steve Naroff218bc2b2007-05-04 21:54:46 +0000911
Steve Naroff75c17232007-06-13 21:41:08 +0000912 if (lType->isPointerType()) {
913 if (rType->isPointerType())
914 return Context.IntTy;
915 if (rType->isIntegerType()) {
Chris Lattner0e9d6222007-07-15 23:26:56 +0000916 if (!rex->isNullPointerConstant(Context))
Steve Naroff75c17232007-06-13 21:41:08 +0000917 Diag(loc, diag::ext_typecheck_comparison_of_pointer_integer,
918 lex->getSourceRange(), rex->getSourceRange());
919 return Context.IntTy; // the previous diagnostic is a GCC extension.
920 }
921 } else if (rType->isPointerType()) {
922 if (lType->isIntegerType()) {
Chris Lattner0e9d6222007-07-15 23:26:56 +0000923 if (!lex->isNullPointerConstant(Context))
Steve Naroff75c17232007-06-13 21:41:08 +0000924 Diag(loc, diag::ext_typecheck_comparison_of_pointer_integer,
925 lex->getSourceRange(), rex->getSourceRange());
926 return Context.IntTy; // the previous diagnostic is a GCC extension.
927 }
Steve Naroff043d45d2007-05-15 02:32:35 +0000928 }
Steve Naroff6f49f5d2007-05-29 14:23:36 +0000929 InvalidOperands(loc, lex, rex);
Steve Naroff218bc2b2007-05-04 21:54:46 +0000930 return QualType();
Steve Naroff26c8ea52007-03-21 21:08:52 +0000931}
932
Steve Naroff218bc2b2007-05-04 21:54:46 +0000933inline QualType Sema::CheckBitwiseOperands(
Steve Naroff7a5af782007-07-13 16:58:59 +0000934 Expr *&lex, Expr *&rex, SourceLocation loc)
Steve Naroff1926c832007-04-24 00:23:05 +0000935{
Steve Naroffd6fbee82007-06-13 15:42:33 +0000936 QualType lhsType = lex->getType(), rhsType = rex->getType();
Steve Naroff84ff4b42007-07-09 21:31:10 +0000937
938 if (lhsType->isVectorType() || rhsType->isVectorType())
939 return CheckVectorOperands(loc, lex, rex);
Steve Naroffb8ea4fb2007-07-13 23:32:42 +0000940
941 QualType resType = UsualArithmeticConversions(lex, rex, lhsType, rhsType);
Steve Naroff1926c832007-04-24 00:23:05 +0000942
Steve Naroff218bc2b2007-05-04 21:54:46 +0000943 if (resType->isIntegerType())
944 return resType;
Steve Naroff6f49f5d2007-05-29 14:23:36 +0000945 InvalidOperands(loc, lex, rex);
Steve Naroff218bc2b2007-05-04 21:54:46 +0000946 return QualType();
Steve Naroff26c8ea52007-03-21 21:08:52 +0000947}
948
Steve Naroff218bc2b2007-05-04 21:54:46 +0000949inline QualType Sema::CheckLogicalOperands( // C99 6.5.[13,14]
Steve Naroff7a5af782007-07-13 16:58:59 +0000950 Expr *&lex, Expr *&rex, SourceLocation loc)
Steve Naroff1926c832007-04-24 00:23:05 +0000951{
Steve Naroff7a5af782007-07-13 16:58:59 +0000952 QualType lhsType = UsualUnaryConversions(lex);
953 QualType rhsType = UsualUnaryConversions(rex);
Steve Naroffe4718892007-04-27 18:30:00 +0000954
Steve Naroff218bc2b2007-05-04 21:54:46 +0000955 if (lhsType->isScalarType() || rhsType->isScalarType())
956 return Context.IntTy;
Steve Naroff6f49f5d2007-05-29 14:23:36 +0000957 InvalidOperands(loc, lex, rex);
Steve Naroff218bc2b2007-05-04 21:54:46 +0000958 return QualType();
Steve Naroffae4143e2007-04-26 20:39:23 +0000959}
960
Steve Naroff35d85152007-05-07 00:24:15 +0000961inline QualType Sema::CheckAssignmentOperands( // C99 6.5.16.1
962 Expr *lex, Expr *rex, SourceLocation loc, QualType compoundType)
Steve Naroffae4143e2007-04-26 20:39:23 +0000963{
Steve Naroff0af91202007-04-27 21:51:21 +0000964 QualType lhsType = lex->getType();
Steve Naroff35d85152007-05-07 00:24:15 +0000965 QualType rhsType = compoundType.isNull() ? rex->getType() : compoundType;
Steve Naroff1f4d7272007-05-11 04:00:31 +0000966 bool hadError = false;
Steve Naroff9358c712007-05-27 23:58:33 +0000967 Expr::isModifiableLvalueResult mlval = lex->isModifiableLvalue();
968
969 switch (mlval) { // C99 6.5.16p2
970 case Expr::MLV_Valid:
971 break;
972 case Expr::MLV_ConstQualified:
973 Diag(loc, diag::err_typecheck_assign_const, lex->getSourceRange());
974 hadError = true;
975 break;
976 case Expr::MLV_ArrayType:
977 Diag(loc, diag::err_typecheck_array_not_modifiable_lvalue,
978 lhsType.getAsString(), lex->getSourceRange());
979 return QualType();
980 case Expr::MLV_NotObjectType:
981 Diag(loc, diag::err_typecheck_non_object_not_modifiable_lvalue,
982 lhsType.getAsString(), lex->getSourceRange());
983 return QualType();
984 case Expr::MLV_InvalidExpression:
985 Diag(loc, diag::err_typecheck_expression_not_modifiable_lvalue,
986 lex->getSourceRange());
987 return QualType();
988 case Expr::MLV_IncompleteType:
989 case Expr::MLV_IncompleteVoidType:
990 Diag(loc, diag::err_typecheck_incomplete_type_not_modifiable_lvalue,
991 lhsType.getAsString(), lex->getSourceRange());
992 return QualType();
Steve Naroff218bc2b2007-05-04 21:54:46 +0000993 }
994 if (lhsType == rhsType) // common case, fast path...
995 return lhsType;
996
Steve Naroffb8ea4fb2007-07-13 23:32:42 +0000997 AssignmentCheckResult result;
998
999 if (compoundType.isNull())
1000 result = CheckSingleAssignmentConstraints(lhsType, rex);
1001 else
1002 result = CheckCompoundAssignmentConstraints(lhsType, rhsType);
1003
Steve Naroff218bc2b2007-05-04 21:54:46 +00001004 // decode the result (notice that extensions still return a type).
1005 switch (result) {
1006 case Compatible:
Steve Naroff1f4d7272007-05-11 04:00:31 +00001007 break;
Steve Naroff218bc2b2007-05-04 21:54:46 +00001008 case Incompatible:
Steve Naroffe845e272007-05-18 01:06:45 +00001009 Diag(loc, diag::err_typecheck_assign_incompatible,
Chris Lattner84e160a2007-05-19 07:03:17 +00001010 lhsType.getAsString(), rhsType.getAsString(),
1011 lex->getSourceRange(), rex->getSourceRange());
Steve Naroff1f4d7272007-05-11 04:00:31 +00001012 hadError = true;
1013 break;
Steve Naroff218bc2b2007-05-04 21:54:46 +00001014 case PointerFromInt:
1015 // check for null pointer constant (C99 6.3.2.3p3)
Chris Lattner0e9d6222007-07-15 23:26:56 +00001016 if (compoundType.isNull() && !rex->isNullPointerConstant(Context)) {
Steve Naroff56faab22007-05-30 04:20:12 +00001017 Diag(loc, diag::ext_typecheck_assign_pointer_int,
1018 lhsType.getAsString(), rhsType.getAsString(),
Steve Naroff9358c712007-05-27 23:58:33 +00001019 lex->getSourceRange(), rex->getSourceRange());
Steve Naroff9992bba2007-05-30 16:27:15 +00001020 }
Steve Naroff1f4d7272007-05-11 04:00:31 +00001021 break;
Steve Naroff56faab22007-05-30 04:20:12 +00001022 case IntFromPointer:
1023 Diag(loc, diag::ext_typecheck_assign_pointer_int,
1024 lhsType.getAsString(), rhsType.getAsString(),
Steve Naroff9358c712007-05-27 23:58:33 +00001025 lex->getSourceRange(), rex->getSourceRange());
Steve Naroff1f4d7272007-05-11 04:00:31 +00001026 break;
Steve Naroff218bc2b2007-05-04 21:54:46 +00001027 case IncompatiblePointer:
Steve Naroff9358c712007-05-27 23:58:33 +00001028 Diag(loc, diag::ext_typecheck_assign_incompatible_pointer,
1029 lhsType.getAsString(), rhsType.getAsString(),
1030 lex->getSourceRange(), rex->getSourceRange());
Steve Naroff1f4d7272007-05-11 04:00:31 +00001031 break;
1032 case CompatiblePointerDiscardsQualifiers:
Steve Naroff9358c712007-05-27 23:58:33 +00001033 Diag(loc, diag::ext_typecheck_assign_discards_qualifiers,
1034 lhsType.getAsString(), rhsType.getAsString(),
1035 lex->getSourceRange(), rex->getSourceRange());
Steve Naroff1f4d7272007-05-11 04:00:31 +00001036 break;
Steve Naroff218bc2b2007-05-04 21:54:46 +00001037 }
Steve Naroff98cf3e92007-06-06 18:38:38 +00001038 // C99 6.5.16p3: The type of an assignment expression is the type of the
1039 // left operand unless the left operand has qualified type, in which case
1040 // it is the unqualified version of the type of the left operand.
1041 // C99 6.5.16.1p2: In simple assignment, the value of the right operand
1042 // is converted to the type of the assignment expression (above).
1043 // C++ 5.17p1: the type of the assignment expression is that of its left oprdu.
1044 return hadError ? QualType() : lhsType.getUnqualifiedType();
Steve Naroffae4143e2007-04-26 20:39:23 +00001045}
1046
Steve Naroff218bc2b2007-05-04 21:54:46 +00001047inline QualType Sema::CheckCommaOperands( // C99 6.5.17
Steve Naroff7a5af782007-07-13 16:58:59 +00001048 Expr *&lex, Expr *&rex, SourceLocation loc) {
1049 return UsualUnaryConversions(rex);
Steve Naroff95af0132007-03-30 23:47:58 +00001050}
1051
Steve Naroff7a5af782007-07-13 16:58:59 +00001052/// CheckIncrementDecrementOperand - unlike most "Check" methods, this routine
1053/// doesn't need to call UsualUnaryConversions or UsualArithmeticConversions.
Steve Naroff71ce2e02007-05-18 22:53:50 +00001054QualType Sema::CheckIncrementDecrementOperand(Expr *op, SourceLocation OpLoc) {
Steve Naroff7a5af782007-07-13 16:58:59 +00001055 QualType resType = op->getType();
Steve Naroff35d85152007-05-07 00:24:15 +00001056 assert(!resType.isNull() && "no type for increment/decrement expression");
Steve Naroffd50c88e2007-04-05 21:15:20 +00001057
Steve Naroff46ba1eb2007-04-03 23:13:13 +00001058 // C99 6.5.2.4p1
Steve Naroff35d85152007-05-07 00:24:15 +00001059 if (const PointerType *pt = dyn_cast<PointerType>(resType)) {
1060 if (!pt->getPointeeType()->isObjectType()) { // C99 6.5.2.4p2, 6.5.6p2
Steve Naroff71ce2e02007-05-18 22:53:50 +00001061 Diag(OpLoc, diag::err_typecheck_arithmetic_incomplete_type,
1062 resType.getAsString(), op->getSourceRange());
Steve Naroff35d85152007-05-07 00:24:15 +00001063 return QualType();
1064 }
1065 } else if (!resType->isRealType()) {
Steve Naroff95af0132007-03-30 23:47:58 +00001066 // FIXME: Allow Complex as a GCC extension.
Steve Naroff71ce2e02007-05-18 22:53:50 +00001067 Diag(OpLoc, diag::err_typecheck_illegal_increment_decrement,
1068 resType.getAsString(), op->getSourceRange());
Steve Naroff35d85152007-05-07 00:24:15 +00001069 return QualType();
Steve Naroff46ba1eb2007-04-03 23:13:13 +00001070 }
Steve Naroff094046f2007-05-13 03:21:25 +00001071 // At this point, we know we have a real or pointer type. Now make sure
1072 // the operand is a modifiable lvalue.
Steve Naroff9358c712007-05-27 23:58:33 +00001073 Expr::isModifiableLvalueResult mlval = op->isModifiableLvalue();
1074 if (mlval != Expr::MLV_Valid) {
1075 // FIXME: emit a more precise diagnostic...
Steve Naroff71ce2e02007-05-18 22:53:50 +00001076 Diag(OpLoc, diag::err_typecheck_invalid_lvalue_incr_decr,
Chris Lattner84e160a2007-05-19 07:03:17 +00001077 op->getSourceRange());
Steve Naroff35d85152007-05-07 00:24:15 +00001078 return QualType();
1079 }
1080 return resType;
Steve Naroff26c8ea52007-03-21 21:08:52 +00001081}
1082
Steve Naroff1926c832007-04-24 00:23:05 +00001083/// getPrimaryDeclaration - Helper function for CheckAddressOfOperand().
Steve Naroff47500512007-04-19 23:00:49 +00001084/// This routine allows us to typecheck complex/recursive expressions
1085/// where the declaration is needed for type checking. Here are some
1086/// examples: &s.xx, &s.zz[1].yy, &(1+2), &(XX), &"123"[2].
Steve Naroff1926c832007-04-24 00:23:05 +00001087static Decl *getPrimaryDeclaration(Expr *e) {
Steve Naroff47500512007-04-19 23:00:49 +00001088 switch (e->getStmtClass()) {
1089 case Stmt::DeclRefExprClass:
1090 return cast<DeclRefExpr>(e)->getDecl();
1091 case Stmt::MemberExprClass:
Steve Naroff1926c832007-04-24 00:23:05 +00001092 return getPrimaryDeclaration(cast<MemberExpr>(e)->getBase());
Steve Naroff47500512007-04-19 23:00:49 +00001093 case Stmt::ArraySubscriptExprClass:
Steve Naroff1926c832007-04-24 00:23:05 +00001094 return getPrimaryDeclaration(cast<ArraySubscriptExpr>(e)->getBase());
Steve Naroff47500512007-04-19 23:00:49 +00001095 case Stmt::CallExprClass:
Steve Naroff1926c832007-04-24 00:23:05 +00001096 return getPrimaryDeclaration(cast<CallExpr>(e)->getCallee());
Steve Naroff47500512007-04-19 23:00:49 +00001097 case Stmt::UnaryOperatorClass:
Steve Naroff1926c832007-04-24 00:23:05 +00001098 return getPrimaryDeclaration(cast<UnaryOperator>(e)->getSubExpr());
Steve Naroff47500512007-04-19 23:00:49 +00001099 case Stmt::ParenExprClass:
Steve Naroff1926c832007-04-24 00:23:05 +00001100 return getPrimaryDeclaration(cast<ParenExpr>(e)->getSubExpr());
Steve Naroff47500512007-04-19 23:00:49 +00001101 default:
1102 return 0;
1103 }
1104}
1105
1106/// CheckAddressOfOperand - The operand of & must be either a function
1107/// designator or an lvalue designating an object. If it is an lvalue, the
1108/// object cannot be declared with storage class register or be a bit field.
1109/// Note: The usual conversions are *not* applied to the operand of the &
Steve Naroffa78fe7e2007-05-16 19:47:19 +00001110/// operator (C99 6.3.2.1p[2-4]), and its result is never an lvalue.
Steve Naroff35d85152007-05-07 00:24:15 +00001111QualType Sema::CheckAddressOfOperand(Expr *op, SourceLocation OpLoc) {
Steve Naroff1926c832007-04-24 00:23:05 +00001112 Decl *dcl = getPrimaryDeclaration(op);
Steve Naroff9358c712007-05-27 23:58:33 +00001113 Expr::isLvalueResult lval = op->isLvalue();
Steve Naroff47500512007-04-19 23:00:49 +00001114
Steve Naroff9358c712007-05-27 23:58:33 +00001115 if (lval != Expr::LV_Valid) { // C99 6.5.3.2p1
Steve Naroff5dd642e2007-05-14 18:14:51 +00001116 if (dcl && isa<FunctionDecl>(dcl)) // allow function designators
1117 ;
Steve Naroff9358c712007-05-27 23:58:33 +00001118 else { // FIXME: emit more specific diag...
Steve Naroff71ce2e02007-05-18 22:53:50 +00001119 Diag(OpLoc, diag::err_typecheck_invalid_lvalue_addrof,
1120 op->getSourceRange());
Steve Naroff35d85152007-05-07 00:24:15 +00001121 return QualType();
1122 }
Steve Naroff47500512007-04-19 23:00:49 +00001123 } else if (dcl) {
1124 // We have an lvalue with a decl. Make sure the decl is not declared
1125 // with the register storage-class specifier.
1126 if (const VarDecl *vd = dyn_cast<VarDecl>(dcl)) {
Steve Naroff35d85152007-05-07 00:24:15 +00001127 if (vd->getStorageClass() == VarDecl::Register) {
Steve Naroff71ce2e02007-05-18 22:53:50 +00001128 Diag(OpLoc, diag::err_typecheck_address_of_register,
Chris Lattner84e160a2007-05-19 07:03:17 +00001129 op->getSourceRange());
Steve Naroff35d85152007-05-07 00:24:15 +00001130 return QualType();
1131 }
Steve Narofff633d092007-04-25 19:01:39 +00001132 } else
1133 assert(0 && "Unknown/unexpected decl type");
1134
Steve Naroff47500512007-04-19 23:00:49 +00001135 // FIXME: add check for bitfields!
1136 }
1137 // If the operand has type "type", the result has type "pointer to type".
Steve Naroff35d85152007-05-07 00:24:15 +00001138 return Context.getPointerType(op->getType());
Steve Naroff47500512007-04-19 23:00:49 +00001139}
1140
Steve Naroff35d85152007-05-07 00:24:15 +00001141QualType Sema::CheckIndirectionOperand(Expr *op, SourceLocation OpLoc) {
Steve Naroff7a5af782007-07-13 16:58:59 +00001142 QualType qType = UsualUnaryConversions(op);
Steve Naroff35d85152007-05-07 00:24:15 +00001143
Steve Naroff47500512007-04-19 23:00:49 +00001144 assert(!qType.isNull() && "no type for * expression");
1145
Steve Naroff758ada12007-05-28 16:15:57 +00001146 if (PointerType *PT = dyn_cast<PointerType>(qType.getCanonicalType())) {
1147 QualType ptype = PT->getPointeeType();
1148 // C99 6.5.3.2p4. "if it points to an object,...".
1149 if (ptype->isIncompleteType()) { // An incomplete type is not an object
1150 // GCC compat: special case 'void *' (treat as warning).
1151 if (ptype->isVoidType()) {
1152 Diag(OpLoc, diag::ext_typecheck_deref_ptr_to_void,
Steve Naroffeb9da942007-05-30 00:06:37 +00001153 qType.getAsString(), op->getSourceRange());
Steve Naroff758ada12007-05-28 16:15:57 +00001154 } else {
1155 Diag(OpLoc, diag::err_typecheck_deref_incomplete_type,
Steve Naroffeb9da942007-05-30 00:06:37 +00001156 ptype.getAsString(), op->getSourceRange());
Steve Naroff758ada12007-05-28 16:15:57 +00001157 return QualType();
1158 }
1159 }
1160 return ptype;
1161 }
1162 Diag(OpLoc, diag::err_typecheck_indirection_requires_pointer,
Steve Naroffeb9da942007-05-30 00:06:37 +00001163 qType.getAsString(), op->getSourceRange());
Steve Naroff35d85152007-05-07 00:24:15 +00001164 return QualType();
Steve Naroff1926c832007-04-24 00:23:05 +00001165}
Steve Naroff218bc2b2007-05-04 21:54:46 +00001166
1167static inline BinaryOperator::Opcode ConvertTokenKindToBinaryOpcode(
1168 tok::TokenKind Kind) {
1169 BinaryOperator::Opcode Opc;
1170 switch (Kind) {
1171 default: assert(0 && "Unknown binop!");
1172 case tok::star: Opc = BinaryOperator::Mul; break;
1173 case tok::slash: Opc = BinaryOperator::Div; break;
1174 case tok::percent: Opc = BinaryOperator::Rem; break;
1175 case tok::plus: Opc = BinaryOperator::Add; break;
1176 case tok::minus: Opc = BinaryOperator::Sub; break;
1177 case tok::lessless: Opc = BinaryOperator::Shl; break;
1178 case tok::greatergreater: Opc = BinaryOperator::Shr; break;
1179 case tok::lessequal: Opc = BinaryOperator::LE; break;
1180 case tok::less: Opc = BinaryOperator::LT; break;
1181 case tok::greaterequal: Opc = BinaryOperator::GE; break;
1182 case tok::greater: Opc = BinaryOperator::GT; break;
1183 case tok::exclaimequal: Opc = BinaryOperator::NE; break;
1184 case tok::equalequal: Opc = BinaryOperator::EQ; break;
1185 case tok::amp: Opc = BinaryOperator::And; break;
1186 case tok::caret: Opc = BinaryOperator::Xor; break;
1187 case tok::pipe: Opc = BinaryOperator::Or; break;
1188 case tok::ampamp: Opc = BinaryOperator::LAnd; break;
1189 case tok::pipepipe: Opc = BinaryOperator::LOr; break;
1190 case tok::equal: Opc = BinaryOperator::Assign; break;
1191 case tok::starequal: Opc = BinaryOperator::MulAssign; break;
1192 case tok::slashequal: Opc = BinaryOperator::DivAssign; break;
1193 case tok::percentequal: Opc = BinaryOperator::RemAssign; break;
1194 case tok::plusequal: Opc = BinaryOperator::AddAssign; break;
1195 case tok::minusequal: Opc = BinaryOperator::SubAssign; break;
1196 case tok::lesslessequal: Opc = BinaryOperator::ShlAssign; break;
1197 case tok::greatergreaterequal: Opc = BinaryOperator::ShrAssign; break;
1198 case tok::ampequal: Opc = BinaryOperator::AndAssign; break;
1199 case tok::caretequal: Opc = BinaryOperator::XorAssign; break;
1200 case tok::pipeequal: Opc = BinaryOperator::OrAssign; break;
1201 case tok::comma: Opc = BinaryOperator::Comma; break;
1202 }
1203 return Opc;
1204}
1205
Steve Naroff35d85152007-05-07 00:24:15 +00001206static inline UnaryOperator::Opcode ConvertTokenKindToUnaryOpcode(
1207 tok::TokenKind Kind) {
1208 UnaryOperator::Opcode Opc;
1209 switch (Kind) {
1210 default: assert(0 && "Unknown unary op!");
1211 case tok::plusplus: Opc = UnaryOperator::PreInc; break;
1212 case tok::minusminus: Opc = UnaryOperator::PreDec; break;
1213 case tok::amp: Opc = UnaryOperator::AddrOf; break;
1214 case tok::star: Opc = UnaryOperator::Deref; break;
1215 case tok::plus: Opc = UnaryOperator::Plus; break;
1216 case tok::minus: Opc = UnaryOperator::Minus; break;
1217 case tok::tilde: Opc = UnaryOperator::Not; break;
1218 case tok::exclaim: Opc = UnaryOperator::LNot; break;
1219 case tok::kw_sizeof: Opc = UnaryOperator::SizeOf; break;
1220 case tok::kw___alignof: Opc = UnaryOperator::AlignOf; break;
1221 case tok::kw___real: Opc = UnaryOperator::Real; break;
1222 case tok::kw___imag: Opc = UnaryOperator::Imag; break;
Chris Lattnerd0f76512007-06-08 22:16:53 +00001223 case tok::kw___extension__: Opc = UnaryOperator::Extension; break;
Steve Naroff35d85152007-05-07 00:24:15 +00001224 }
1225 return Opc;
1226}
1227
Steve Naroff218bc2b2007-05-04 21:54:46 +00001228// Binary Operators. 'Tok' is the token for the operator.
1229Action::ExprResult Sema::ParseBinOp(SourceLocation TokLoc, tok::TokenKind Kind,
1230 ExprTy *LHS, ExprTy *RHS) {
1231 BinaryOperator::Opcode Opc = ConvertTokenKindToBinaryOpcode(Kind);
1232 Expr *lhs = (Expr *)LHS, *rhs = (Expr*)RHS;
1233
1234 assert((lhs != 0) && "ParseBinOp(): missing left expression");
1235 assert((rhs != 0) && "ParseBinOp(): missing right expression");
1236
Chris Lattner256c21b2007-06-28 03:53:10 +00001237 QualType ResultTy; // Result type of the binary operator.
1238 QualType CompTy; // Computation type for compound assignments (e.g. '+=')
Steve Naroff218bc2b2007-05-04 21:54:46 +00001239
1240 switch (Opc) {
1241 default:
1242 assert(0 && "Unknown binary expr!");
1243 case BinaryOperator::Assign:
Chris Lattner256c21b2007-06-28 03:53:10 +00001244 ResultTy = CheckAssignmentOperands(lhs, rhs, TokLoc, QualType());
Steve Naroff218bc2b2007-05-04 21:54:46 +00001245 break;
1246 case BinaryOperator::Mul:
1247 case BinaryOperator::Div:
Chris Lattner256c21b2007-06-28 03:53:10 +00001248 ResultTy = CheckMultiplyDivideOperands(lhs, rhs, TokLoc);
Steve Naroff218bc2b2007-05-04 21:54:46 +00001249 break;
1250 case BinaryOperator::Rem:
Chris Lattner256c21b2007-06-28 03:53:10 +00001251 ResultTy = CheckRemainderOperands(lhs, rhs, TokLoc);
Steve Naroff218bc2b2007-05-04 21:54:46 +00001252 break;
1253 case BinaryOperator::Add:
Chris Lattner256c21b2007-06-28 03:53:10 +00001254 ResultTy = CheckAdditionOperands(lhs, rhs, TokLoc);
Steve Naroff218bc2b2007-05-04 21:54:46 +00001255 break;
1256 case BinaryOperator::Sub:
Chris Lattner256c21b2007-06-28 03:53:10 +00001257 ResultTy = CheckSubtractionOperands(lhs, rhs, TokLoc);
Steve Naroff218bc2b2007-05-04 21:54:46 +00001258 break;
1259 case BinaryOperator::Shl:
1260 case BinaryOperator::Shr:
Chris Lattner256c21b2007-06-28 03:53:10 +00001261 ResultTy = CheckShiftOperands(lhs, rhs, TokLoc);
Steve Naroff218bc2b2007-05-04 21:54:46 +00001262 break;
1263 case BinaryOperator::LE:
1264 case BinaryOperator::LT:
1265 case BinaryOperator::GE:
1266 case BinaryOperator::GT:
Chris Lattner256c21b2007-06-28 03:53:10 +00001267 ResultTy = CheckRelationalOperands(lhs, rhs, TokLoc);
Steve Naroff218bc2b2007-05-04 21:54:46 +00001268 break;
1269 case BinaryOperator::EQ:
1270 case BinaryOperator::NE:
Chris Lattner256c21b2007-06-28 03:53:10 +00001271 ResultTy = CheckEqualityOperands(lhs, rhs, TokLoc);
Steve Naroff218bc2b2007-05-04 21:54:46 +00001272 break;
1273 case BinaryOperator::And:
1274 case BinaryOperator::Xor:
1275 case BinaryOperator::Or:
Chris Lattner256c21b2007-06-28 03:53:10 +00001276 ResultTy = CheckBitwiseOperands(lhs, rhs, TokLoc);
Steve Naroff218bc2b2007-05-04 21:54:46 +00001277 break;
1278 case BinaryOperator::LAnd:
1279 case BinaryOperator::LOr:
Chris Lattner256c21b2007-06-28 03:53:10 +00001280 ResultTy = CheckLogicalOperands(lhs, rhs, TokLoc);
Steve Naroff218bc2b2007-05-04 21:54:46 +00001281 break;
1282 case BinaryOperator::MulAssign:
1283 case BinaryOperator::DivAssign:
Chris Lattner256c21b2007-06-28 03:53:10 +00001284 CompTy = CheckMultiplyDivideOperands(lhs, rhs, TokLoc);
1285 if (!CompTy.isNull())
1286 ResultTy = CheckAssignmentOperands(lhs, rhs, TokLoc, CompTy);
Steve Naroff218bc2b2007-05-04 21:54:46 +00001287 break;
1288 case BinaryOperator::RemAssign:
Chris Lattner256c21b2007-06-28 03:53:10 +00001289 CompTy = CheckRemainderOperands(lhs, rhs, TokLoc);
1290 if (!CompTy.isNull())
1291 ResultTy = CheckAssignmentOperands(lhs, rhs, TokLoc, CompTy);
Steve Naroff218bc2b2007-05-04 21:54:46 +00001292 break;
1293 case BinaryOperator::AddAssign:
Chris Lattner256c21b2007-06-28 03:53:10 +00001294 CompTy = CheckAdditionOperands(lhs, rhs, TokLoc);
1295 if (!CompTy.isNull())
1296 ResultTy = CheckAssignmentOperands(lhs, rhs, TokLoc, CompTy);
Steve Naroff218bc2b2007-05-04 21:54:46 +00001297 break;
1298 case BinaryOperator::SubAssign:
Chris Lattner256c21b2007-06-28 03:53:10 +00001299 CompTy = CheckSubtractionOperands(lhs, rhs, TokLoc);
1300 if (!CompTy.isNull())
1301 ResultTy = CheckAssignmentOperands(lhs, rhs, TokLoc, CompTy);
Steve Naroff218bc2b2007-05-04 21:54:46 +00001302 break;
1303 case BinaryOperator::ShlAssign:
1304 case BinaryOperator::ShrAssign:
Chris Lattner256c21b2007-06-28 03:53:10 +00001305 CompTy = CheckShiftOperands(lhs, rhs, TokLoc);
1306 if (!CompTy.isNull())
1307 ResultTy = CheckAssignmentOperands(lhs, rhs, TokLoc, CompTy);
Steve Naroff218bc2b2007-05-04 21:54:46 +00001308 break;
1309 case BinaryOperator::AndAssign:
1310 case BinaryOperator::XorAssign:
1311 case BinaryOperator::OrAssign:
Chris Lattner256c21b2007-06-28 03:53:10 +00001312 CompTy = CheckBitwiseOperands(lhs, rhs, TokLoc);
1313 if (!CompTy.isNull())
1314 ResultTy = CheckAssignmentOperands(lhs, rhs, TokLoc, CompTy);
Steve Naroff218bc2b2007-05-04 21:54:46 +00001315 break;
1316 case BinaryOperator::Comma:
Chris Lattner256c21b2007-06-28 03:53:10 +00001317 ResultTy = CheckCommaOperands(lhs, rhs, TokLoc);
Steve Naroff218bc2b2007-05-04 21:54:46 +00001318 break;
1319 }
Chris Lattner256c21b2007-06-28 03:53:10 +00001320 if (ResultTy.isNull())
Steve Naroff218bc2b2007-05-04 21:54:46 +00001321 return true;
Chris Lattner256c21b2007-06-28 03:53:10 +00001322 if (CompTy.isNull())
1323 return new BinaryOperator(lhs, rhs, Opc, ResultTy);
1324 else
Chris Lattner9369a562007-06-29 16:31:29 +00001325 return new CompoundAssignOperator(lhs, rhs, Opc, ResultTy, CompTy);
Steve Naroff218bc2b2007-05-04 21:54:46 +00001326}
1327
Steve Naroff35d85152007-05-07 00:24:15 +00001328// Unary Operators. 'Tok' is the token for the operator.
1329Action::ExprResult Sema::ParseUnaryOp(SourceLocation OpLoc, tok::TokenKind Op,
Chris Lattner86554282007-06-08 22:32:33 +00001330 ExprTy *input) {
1331 Expr *Input = (Expr*)input;
Steve Naroff35d85152007-05-07 00:24:15 +00001332 UnaryOperator::Opcode Opc = ConvertTokenKindToUnaryOpcode(Op);
1333 QualType resultType;
1334 switch (Opc) {
1335 default:
1336 assert(0 && "Unimplemented unary expr!");
1337 case UnaryOperator::PreInc:
1338 case UnaryOperator::PreDec:
Chris Lattner86554282007-06-08 22:32:33 +00001339 resultType = CheckIncrementDecrementOperand(Input, OpLoc);
Steve Naroff35d85152007-05-07 00:24:15 +00001340 break;
1341 case UnaryOperator::AddrOf:
Chris Lattner86554282007-06-08 22:32:33 +00001342 resultType = CheckAddressOfOperand(Input, OpLoc);
Steve Naroff35d85152007-05-07 00:24:15 +00001343 break;
1344 case UnaryOperator::Deref:
Chris Lattner86554282007-06-08 22:32:33 +00001345 resultType = CheckIndirectionOperand(Input, OpLoc);
Steve Naroff35d85152007-05-07 00:24:15 +00001346 break;
1347 case UnaryOperator::Plus:
1348 case UnaryOperator::Minus:
Steve Naroff7a5af782007-07-13 16:58:59 +00001349 resultType = UsualUnaryConversions(Input);
Steve Naroff35d85152007-05-07 00:24:15 +00001350 if (!resultType->isArithmeticType()) // C99 6.5.3.3p1
Chris Lattnerc04bd6a2007-05-16 18:09:54 +00001351 return Diag(OpLoc, diag::err_typecheck_unary_expr,
1352 resultType.getAsString());
Steve Naroff35d85152007-05-07 00:24:15 +00001353 break;
1354 case UnaryOperator::Not: // bitwise complement
Steve Naroff7a5af782007-07-13 16:58:59 +00001355 resultType = UsualUnaryConversions(Input);
Steve Naroff1c8b7d32007-07-12 21:46:55 +00001356 if (!resultType->isIntegerType()) // C99 6.5.3.3p1
1357 return Diag(OpLoc, diag::err_typecheck_unary_expr,
1358 resultType.getAsString());
Steve Naroff35d85152007-05-07 00:24:15 +00001359 break;
1360 case UnaryOperator::LNot: // logical negation
Steve Naroff71b59a92007-06-04 22:22:31 +00001361 // Unlike +/-/~, integer promotions aren't done here (C99 6.5.3.3p5).
Steve Naroffb8ea4fb2007-07-13 23:32:42 +00001362 resultType = DefaultFunctionArrayConversion(Input);
Steve Naroff35d85152007-05-07 00:24:15 +00001363 if (!resultType->isScalarType()) // C99 6.5.3.3p1
Chris Lattnerc04bd6a2007-05-16 18:09:54 +00001364 return Diag(OpLoc, diag::err_typecheck_unary_expr,
1365 resultType.getAsString());
Chris Lattnerbe31ed82007-06-02 19:11:33 +00001366 // LNot always has type int. C99 6.5.3.3p5.
1367 resultType = Context.IntTy;
Steve Naroff35d85152007-05-07 00:24:15 +00001368 break;
1369 case UnaryOperator::SizeOf:
Chris Lattner86554282007-06-08 22:32:33 +00001370 resultType = CheckSizeOfAlignOfOperand(Input->getType(), OpLoc, true);
Steve Naroff043d45d2007-05-15 02:32:35 +00001371 break;
Steve Naroff35d85152007-05-07 00:24:15 +00001372 case UnaryOperator::AlignOf:
Chris Lattner86554282007-06-08 22:32:33 +00001373 resultType = CheckSizeOfAlignOfOperand(Input->getType(), OpLoc, false);
1374 break;
1375 case UnaryOperator::Extension:
1376 // FIXME: does __extension__ cause any promotions? I would think not.
1377 resultType = Input->getType();
Steve Naroff043d45d2007-05-15 02:32:35 +00001378 break;
Steve Naroff35d85152007-05-07 00:24:15 +00001379 }
1380 if (resultType.isNull())
1381 return true;
Chris Lattner86554282007-06-08 22:32:33 +00001382 return new UnaryOperator(Input, Opc, resultType, OpLoc);
Steve Naroff35d85152007-05-07 00:24:15 +00001383}
Chris Lattnereefa10e2007-05-28 06:56:27 +00001384
1385/// ParseAddrLabel - Parse the GNU address of label extension: "&&foo".
1386Sema::ExprResult Sema::ParseAddrLabel(SourceLocation OpLoc,
1387 SourceLocation LabLoc,
1388 IdentifierInfo *LabelII) {
1389 // Look up the record for this label identifier.
1390 LabelStmt *&LabelDecl = LabelMap[LabelII];
1391
1392 // If we haven't seen this label yet, create a forward reference.
1393 if (LabelDecl == 0)
1394 LabelDecl = new LabelStmt(LabLoc, LabelII, 0);
1395
1396 // Create the AST node. The address of a label always has type 'void*'.
1397 return new AddrLabel(OpLoc, LabLoc, LabelDecl,
1398 Context.getPointerType(Context.VoidTy));
1399}
1400