blob: 60b07a7915ba6014113de59497988bd6a97df5df [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) {
Steve Naroffb8ea4fb2007-07-13 23:32:42 +0000282 Expr *lex = (Expr *)Base;
283 Expr *rex = (Expr *)Idx;
284 QualType t1 = lex->getType();
285 QualType t2 = rex->getType();
Steve Narofff1e53692007-03-23 22:27:02 +0000286
287 assert(!t1.isNull() && "no type for array base expression");
Steve Naroffc1aadb12007-03-28 21:49:40 +0000288 assert(!t2.isNull() && "no type for array index expression");
Steve Narofff1e53692007-03-23 22:27:02 +0000289
Steve Naroffb8ea4fb2007-07-13 23:32:42 +0000290 QualType canonT1 = DefaultFunctionArrayConversion(lex).getCanonicalType();
291 QualType canonT2 = DefaultFunctionArrayConversion(rex).getCanonicalType();
Steve Naroffd50c88e2007-04-05 21:15:20 +0000292
Steve Naroffc1aadb12007-03-28 21:49:40 +0000293 // C99 6.5.2.1p2: the expression e1[e2] is by definition precisely equivalent
294 // to the expression *((e1)+(e2)). This means the array "Base" may actually be
Steve Narofff1e53692007-03-23 22:27:02 +0000295 // in the subscript position. As a result, we need to derive the array base
296 // and index from the expression types.
297
Steve Naroffb3096442007-06-09 03:47:53 +0000298 Expr *baseExpr, *indexExpr;
Steve Naroffe5aa9be2007-04-05 22:36:20 +0000299 QualType baseType, indexType;
Steve Naroffb29cdd52007-07-10 18:23:31 +0000300 if (isa<PointerType>(canonT1) || isa<VectorType>(canonT1)) {
Steve Naroffd50c88e2007-04-05 21:15:20 +0000301 baseType = canonT1;
302 indexType = canonT2;
Steve Naroffb8ea4fb2007-07-13 23:32:42 +0000303 baseExpr = lex;
304 indexExpr = rex;
Steve Naroffb3096442007-06-09 03:47:53 +0000305 } else if (isa<PointerType>(canonT2)) { // uncommon
Steve Naroffd50c88e2007-04-05 21:15:20 +0000306 baseType = canonT2;
307 indexType = canonT1;
Steve Naroffb8ea4fb2007-07-13 23:32:42 +0000308 baseExpr = rex;
309 indexExpr = lex;
Steve Naroffb3096442007-06-09 03:47:53 +0000310 } else {
Steve Naroffb8ea4fb2007-07-13 23:32:42 +0000311 return Diag(lex->getLocStart(), diag::err_typecheck_subscript_value,
312 rex->getSourceRange());
Steve Naroffb3096442007-06-09 03:47:53 +0000313 }
Steve Naroffc1aadb12007-03-28 21:49:40 +0000314 // C99 6.5.2.1p1
Steve Naroffb3096442007-06-09 03:47:53 +0000315 if (!indexType->isIntegerType()) {
316 return Diag(indexExpr->getLocStart(), diag::err_typecheck_subscript,
317 indexExpr->getSourceRange());
318 }
Steve Naroffb29cdd52007-07-10 18:23:31 +0000319 QualType resultType;
320 if (PointerType *ary = dyn_cast<PointerType>(baseType)) {
321 // FIXME: need to deal with const...
322 resultType = ary->getPointeeType();
323 // in practice, the following check catches trying to index a pointer
324 // to a function (e.g. void (*)(int)). Functions are not objects in c99.
325 if (!resultType->isObjectType()) {
326 return Diag(baseExpr->getLocStart(),
327 diag::err_typecheck_subscript_not_object,
328 baseType.getAsString(), baseExpr->getSourceRange());
329 }
330 } else if (VectorType *vec = dyn_cast<VectorType>(baseType))
331 resultType = vec->getElementType();
332
Steve Naroff509fe022007-05-17 01:16:00 +0000333 return new ArraySubscriptExpr((Expr*)Base, (Expr*)Idx, resultType, RLoc);
Chris Lattnere168f762006-11-10 05:29:30 +0000334}
335
336Action::ExprResult Sema::
337ParseMemberReferenceExpr(ExprTy *Base, SourceLocation OpLoc,
338 tok::TokenKind OpKind, SourceLocation MemberLoc,
339 IdentifierInfo &Member) {
Steve Naroffe5aa9be2007-04-05 22:36:20 +0000340 QualType qualifiedType = ((Expr *)Base)->getType();
Steve Naroffca8f7122007-04-01 01:41:35 +0000341
342 assert(!qualifiedType.isNull() && "no type for member expression");
343
Steve Naroffe5aa9be2007-04-05 22:36:20 +0000344 QualType canonType = qualifiedType.getCanonicalType();
Steve Narofff1e53692007-03-23 22:27:02 +0000345
346 if (OpKind == tok::arrow) {
Steve Naroffca8f7122007-04-01 01:41:35 +0000347 if (PointerType *PT = dyn_cast<PointerType>(canonType)) {
348 qualifiedType = PT->getPointeeType();
Steve Naroffd50c88e2007-04-05 21:15:20 +0000349 canonType = qualifiedType.getCanonicalType();
Steve Naroffca8f7122007-04-01 01:41:35 +0000350 } else
Steve Narofff1e53692007-03-23 22:27:02 +0000351 return Diag(OpLoc, diag::err_typecheck_member_reference_arrow);
352 }
Steve Naroff92e30f82007-04-02 22:35:25 +0000353 if (!isa<RecordType>(canonType))
354 return Diag(OpLoc, diag::err_typecheck_member_reference_structUnion);
355
356 // get the struct/union definition from the type.
357 RecordDecl *RD = cast<RecordType>(canonType)->getDecl();
Steve Naroffcc321422007-03-26 23:09:51 +0000358
Steve Naroff92e30f82007-04-02 22:35:25 +0000359 if (canonType->isIncompleteType())
360 return Diag(OpLoc, diag::err_typecheck_incomplete_tag, RD->getName());
Steve Naroffcc321422007-03-26 23:09:51 +0000361
Steve Naroff92e30f82007-04-02 22:35:25 +0000362 FieldDecl *MemberDecl = RD->getMember(&Member);
363 if (!MemberDecl)
364 return Diag(OpLoc, diag::err_typecheck_no_member, Member.getName());
365
Steve Naroff9358c712007-05-27 23:58:33 +0000366 return new MemberExpr((Expr*)Base, OpKind == tok::arrow,
367 MemberDecl, MemberLoc);
Chris Lattnere168f762006-11-10 05:29:30 +0000368}
369
370/// ParseCallExpr - Handle a call to Fn with the specified array of arguments.
371/// This provides the location of the left/right parens and a list of comma
372/// locations.
373Action::ExprResult Sema::
374ParseCallExpr(ExprTy *Fn, SourceLocation LParenLoc,
Steve Naroffb8c289d2007-05-08 22:18:00 +0000375 ExprTy **Args, unsigned NumArgsInCall,
Chris Lattnere168f762006-11-10 05:29:30 +0000376 SourceLocation *CommaLocs, SourceLocation RParenLoc) {
Steve Naroff8563f652007-05-28 19:25:56 +0000377 Expr *funcExpr = (Expr *)Fn;
Steve Naroff8563f652007-05-28 19:25:56 +0000378 assert(funcExpr && "no function call expression");
379
Steve Naroff7a5af782007-07-13 16:58:59 +0000380 QualType qType = UsualUnaryConversions(funcExpr);
Steve Naroffae4143e2007-04-26 20:39:23 +0000381 assert(!qType.isNull() && "no type for function call expression");
382
Chris Lattner3d01e4e2007-06-06 05:14:05 +0000383 // C99 6.5.2.2p1 - "The expression that denotes the called function shall have
384 // type pointer to function".
385 const PointerType *PT = dyn_cast<PointerType>(qType);
386 if (PT == 0) PT = dyn_cast<PointerType>(qType.getCanonicalType());
387
388 if (PT == 0)
389 return Diag(funcExpr->getLocStart(), diag::err_typecheck_call_not_function,
390 SourceRange(funcExpr->getLocStart(), RParenLoc));
Chris Lattner3343f812007-06-06 05:05:41 +0000391
392 const FunctionType *funcT = dyn_cast<FunctionType>(PT->getPointeeType());
Chris Lattner3d01e4e2007-06-06 05:14:05 +0000393 if (funcT == 0)
394 funcT = dyn_cast<FunctionType>(PT->getPointeeType().getCanonicalType());
395
396 if (funcT == 0)
397 return Diag(funcExpr->getLocStart(), diag::err_typecheck_call_not_function,
398 SourceRange(funcExpr->getLocStart(), RParenLoc));
Steve Naroffb8c289d2007-05-08 22:18:00 +0000399
Steve Naroff17f76e02007-05-03 21:03:48 +0000400 // If a prototype isn't declared, the parser implicitly defines a func decl
401 QualType resultType = funcT->getResultType();
402
403 if (const FunctionTypeProto *proto = dyn_cast<FunctionTypeProto>(funcT)) {
404 // C99 6.5.2.2p7 - the arguments are implicitly converted, as if by
405 // assignment, to the types of the corresponding parameter, ...
406
407 unsigned NumArgsInProto = proto->getNumArgs();
Steve Naroffb8c289d2007-05-08 22:18:00 +0000408 unsigned NumArgsToCheck = NumArgsInCall;
Steve Naroff17f76e02007-05-03 21:03:48 +0000409
Steve Naroffb8c289d2007-05-08 22:18:00 +0000410 if (NumArgsInCall < NumArgsInProto)
Steve Naroff56faab22007-05-30 04:20:12 +0000411 Diag(RParenLoc, diag::err_typecheck_call_too_few_args,
Steve Naroffeb9da942007-05-30 00:06:37 +0000412 funcExpr->getSourceRange());
Steve Naroffb8c289d2007-05-08 22:18:00 +0000413 else if (NumArgsInCall > NumArgsInProto) {
Steve Naroff8563f652007-05-28 19:25:56 +0000414 if (!proto->isVariadic()) {
Steve Naroff56faab22007-05-30 04:20:12 +0000415 Diag(((Expr **)Args)[NumArgsInProto+1]->getLocStart(),
416 diag::err_typecheck_call_too_many_args, funcExpr->getSourceRange(),
417 ((Expr **)Args)[NumArgsInProto+1]->getSourceRange());
Steve Naroff8563f652007-05-28 19:25:56 +0000418 }
Steve Naroffb8c289d2007-05-08 22:18:00 +0000419 NumArgsToCheck = NumArgsInProto;
Steve Naroff17f76e02007-05-03 21:03:48 +0000420 }
421 // Continue to check argument types (even if we have too few/many args).
Steve Naroffb8c289d2007-05-08 22:18:00 +0000422 for (unsigned i = 0; i < NumArgsToCheck; i++) {
Steve Naroff8563f652007-05-28 19:25:56 +0000423 Expr *argExpr = ((Expr **)Args)[i];
Steve Naroff8563f652007-05-28 19:25:56 +0000424 assert(argExpr && "ParseCallExpr(): missing argument expression");
425
Steve Naroff17f76e02007-05-03 21:03:48 +0000426 QualType lhsType = proto->getArgType(i);
Steve Naroff8563f652007-05-28 19:25:56 +0000427 QualType rhsType = argExpr->getType();
Steve Naroff17f76e02007-05-03 21:03:48 +0000428
429 if (lhsType == rhsType) // common case, fast path...
430 continue;
Steve Naroff98cf3e92007-06-06 18:38:38 +0000431
Steve Naroffb8ea4fb2007-07-13 23:32:42 +0000432 AssignmentCheckResult result = CheckSingleAssignmentConstraints(lhsType,
433 argExpr);
Steve Naroff6f49f5d2007-05-29 14:23:36 +0000434 SourceLocation l = argExpr->getLocStart();
Steve Naroff17f76e02007-05-03 21:03:48 +0000435
436 // decode the result (notice that AST's are still created for extensions).
Steve Naroff17f76e02007-05-03 21:03:48 +0000437 switch (result) {
438 case Compatible:
439 break;
440 case PointerFromInt:
Steve Naroff218bc2b2007-05-04 21:54:46 +0000441 // check for null pointer constant (C99 6.3.2.3p3)
Steve Naroffeb9da942007-05-30 00:06:37 +0000442 if (!argExpr->isNullPointerConstant()) {
Steve Naroff56faab22007-05-30 04:20:12 +0000443 Diag(l, diag::ext_typecheck_passing_pointer_int,
444 lhsType.getAsString(), rhsType.getAsString(),
Steve Naroffeb9da942007-05-30 00:06:37 +0000445 funcExpr->getSourceRange(), argExpr->getSourceRange());
446 }
Steve Naroff17f76e02007-05-03 21:03:48 +0000447 break;
448 case IntFromPointer:
Steve Naroff56faab22007-05-30 04:20:12 +0000449 Diag(l, diag::ext_typecheck_passing_pointer_int,
450 lhsType.getAsString(), rhsType.getAsString(),
Steve Naroffeb9da942007-05-30 00:06:37 +0000451 funcExpr->getSourceRange(), argExpr->getSourceRange());
Steve Naroff17f76e02007-05-03 21:03:48 +0000452 break;
453 case IncompatiblePointer:
Steve Naroff8563f652007-05-28 19:25:56 +0000454 Diag(l, diag::ext_typecheck_passing_incompatible_pointer,
Steve Naroffeb9da942007-05-30 00:06:37 +0000455 rhsType.getAsString(), lhsType.getAsString(),
456 funcExpr->getSourceRange(), argExpr->getSourceRange());
Steve Naroff17f76e02007-05-03 21:03:48 +0000457 break;
Steve Naroff1f4d7272007-05-11 04:00:31 +0000458 case CompatiblePointerDiscardsQualifiers:
Steve Naroffeb9da942007-05-30 00:06:37 +0000459 Diag(l, diag::ext_typecheck_passing_discards_qualifiers,
460 rhsType.getAsString(), lhsType.getAsString(),
461 funcExpr->getSourceRange(), argExpr->getSourceRange());
Steve Naroff1f4d7272007-05-11 04:00:31 +0000462 break;
Steve Naroff17f76e02007-05-03 21:03:48 +0000463 case Incompatible:
Steve Naroff8563f652007-05-28 19:25:56 +0000464 return Diag(l, diag::err_typecheck_passing_incompatible,
465 rhsType.getAsString(), lhsType.getAsString(),
466 funcExpr->getSourceRange(), argExpr->getSourceRange());
Steve Naroff17f76e02007-05-03 21:03:48 +0000467 }
468 }
Steve Naroffb8c289d2007-05-08 22:18:00 +0000469 // Even if the types checked, bail if we had the wrong number of arguments.
470 if ((NumArgsInCall != NumArgsInProto) && !proto->isVariadic())
471 return true;
Steve Naroffae4143e2007-04-26 20:39:23 +0000472 }
Steve Naroff509fe022007-05-17 01:16:00 +0000473 return new CallExpr((Expr*)Fn, (Expr**)Args, NumArgsInCall, resultType,
474 RParenLoc);
Chris Lattnere168f762006-11-10 05:29:30 +0000475}
476
477Action::ExprResult Sema::
478ParseCastExpr(SourceLocation LParenLoc, TypeTy *Ty,
479 SourceLocation RParenLoc, ExprTy *Op) {
Chris Lattner0d8b1a12006-11-20 04:34:45 +0000480 // If error parsing type, ignore.
Steve Naroffae4143e2007-04-26 20:39:23 +0000481 assert((Ty != 0) && "ParseCastExpr(): missing type");
Chris Lattner1d411a82007-06-05 20:52:21 +0000482 // FIXME: Sema for cast is completely missing.
Steve Naroff509fe022007-05-17 01:16:00 +0000483 return new CastExpr(QualType::getFromOpaquePtr(Ty), (Expr*)Op, LParenLoc);
Chris Lattnere168f762006-11-10 05:29:30 +0000484}
485
Steve Narofff8a28c52007-05-15 20:29:32 +0000486inline QualType Sema::CheckConditionalOperands( // C99 6.5.15
Steve Naroff7a5af782007-07-13 16:58:59 +0000487 Expr *&cond, Expr *&lex, Expr *&rex, SourceLocation questionLoc) {
Steve Naroffb66fb742007-07-13 17:39:21 +0000488 QualType condT = UsualUnaryConversions(cond);
489 QualType lexT = UsualUnaryConversions(lex);
490 QualType rexT = UsualUnaryConversions(rex);
Steve Naroffa78fe7e2007-05-16 19:47:19 +0000491
492 // first, check the condition.
Steve Naroff7a5af782007-07-13 16:58:59 +0000493 if (!condT->isScalarType()) { // C99 6.5.15p2
494 Diag(cond->getLocStart(), diag::err_typecheck_cond_expect_scalar,
495 condT.getAsString());
Steve Naroffa78fe7e2007-05-16 19:47:19 +0000496 return QualType();
497 }
498 // now check the two expressions.
Steve Naroff7a5af782007-07-13 16:58:59 +0000499 if (lexT->isArithmeticType() && rexT->isArithmeticType()) // C99 6.5.15p3,5
Steve Naroffb8ea4fb2007-07-13 23:32:42 +0000500 return UsualArithmeticConversions(lex, rex, lexT, rexT);
Steve Naroffa78fe7e2007-05-16 19:47:19 +0000501
Steve Naroff7a5af782007-07-13 16:58:59 +0000502 if ((lexT->isStructureType() && rexT->isStructureType()) || // C99 6.5.15p3
503 (lexT->isUnionType() && rexT->isUnionType())) {
504 TagType *lTag = cast<TagType>(lexT.getCanonicalType());
505 TagType *rTag = cast<TagType>(rexT.getCanonicalType());
Steve Naroffa78fe7e2007-05-16 19:47:19 +0000506
507 if (lTag->getDecl()->getIdentifier() == rTag->getDecl()->getIdentifier())
Steve Naroff7a5af782007-07-13 16:58:59 +0000508 return lexT;
Steve Naroffa78fe7e2007-05-16 19:47:19 +0000509 else {
510 Diag(questionLoc, diag::err_typecheck_cond_incompatible_operands,
Steve Naroff7a5af782007-07-13 16:58:59 +0000511 lexT.getAsString(), rexT.getAsString(),
512 lex->getSourceRange(), rex->getSourceRange());
Steve Naroffa78fe7e2007-05-16 19:47:19 +0000513 return QualType();
514 }
515 }
Steve Naroff7a5af782007-07-13 16:58:59 +0000516 if (lexT->isPointerType() && rex->isNullPointerConstant()) // C99 6.5.15p3
517 return lexT;
518 if (rexT->isPointerType() && lex->isNullPointerConstant())
519 return rexT;
Steve Naroff30d1fbc2007-05-20 19:46:53 +0000520
Steve Naroff7a5af782007-07-13 16:58:59 +0000521 if (lexT->isPointerType() && rexT->isPointerType()) { // C99 6.5.15p3,6
Steve Naroffa78fe7e2007-05-16 19:47:19 +0000522 QualType lhptee, rhptee;
523
524 // get the "pointed to" type
Steve Naroff7a5af782007-07-13 16:58:59 +0000525 lhptee = cast<PointerType>(lexT.getCanonicalType())->getPointeeType();
526 rhptee = cast<PointerType>(rexT.getCanonicalType())->getPointeeType();
Steve Naroffa78fe7e2007-05-16 19:47:19 +0000527
528 // ignore qualifiers on void (C99 6.5.15p3, clause 6)
529 if (lhptee.getUnqualifiedType()->isVoidType() &&
530 (rhptee->isObjectType() || rhptee->isIncompleteType()))
Steve Naroff7a5af782007-07-13 16:58:59 +0000531 return lexT;
Steve Naroffa78fe7e2007-05-16 19:47:19 +0000532 if (rhptee.getUnqualifiedType()->isVoidType() &&
533 (lhptee->isObjectType() || lhptee->isIncompleteType()))
Steve Naroff7a5af782007-07-13 16:58:59 +0000534 return rexT;
Steve Naroffa78fe7e2007-05-16 19:47:19 +0000535
536 // FIXME: C99 6.5.15p6: If both operands are pointers to compatible types
537 // *or* to differently qualified versions of compatible types, the result
538 // type is a pointer to an appropriately qualified version of the
539 // *composite* type.
540 if (!Type::typesAreCompatible(lhptee.getUnqualifiedType(),
541 rhptee.getUnqualifiedType())) {
542 Diag(questionLoc, diag::ext_typecheck_cond_incompatible_pointers,
Steve Naroff7a5af782007-07-13 16:58:59 +0000543 lexT.getAsString(), rexT.getAsString(),
544 lex->getSourceRange(), rex->getSourceRange());
545 return lexT; // FIXME: this is an _ext - is this return o.k?
Steve Naroffa78fe7e2007-05-16 19:47:19 +0000546 }
547 }
Steve Naroff7a5af782007-07-13 16:58:59 +0000548 if (lexT->isVoidType() && rexT->isVoidType()) // C99 6.5.15p3
549 return lexT;
Steve Naroffa78fe7e2007-05-16 19:47:19 +0000550
551 Diag(questionLoc, diag::err_typecheck_cond_incompatible_operands,
Steve Naroff7a5af782007-07-13 16:58:59 +0000552 lexT.getAsString(), rexT.getAsString(),
553 lex->getSourceRange(), rex->getSourceRange());
Steve Naroffa78fe7e2007-05-16 19:47:19 +0000554 return QualType();
Steve Narofff8a28c52007-05-15 20:29:32 +0000555}
556
Chris Lattnere168f762006-11-10 05:29:30 +0000557/// ParseConditionalOp - Parse a ?: operation. Note that 'LHS' may be null
558/// in the case of a the GNU conditional expr extension.
559Action::ExprResult Sema::ParseConditionalOp(SourceLocation QuestionLoc,
560 SourceLocation ColonLoc,
561 ExprTy *Cond, ExprTy *LHS,
562 ExprTy *RHS) {
Steve Naroff7a5af782007-07-13 16:58:59 +0000563 QualType result = CheckConditionalOperands((Expr *&)Cond, (Expr *&)LHS,
564 (Expr *&)RHS, QuestionLoc);
Steve Narofff8a28c52007-05-15 20:29:32 +0000565 if (result.isNull())
566 return true;
567 return new ConditionalOperator((Expr*)Cond, (Expr*)LHS, (Expr*)RHS, result);
Chris Lattnere168f762006-11-10 05:29:30 +0000568}
569
Steve Naroffb8ea4fb2007-07-13 23:32:42 +0000570QualType Sema::DefaultFunctionArrayConversion(Expr *&expr) {
571 QualType t = expr->getType();
572 assert(!t.isNull() && "DefaultFunctionArrayConversion - missing type");
573
Steve Naroffa78fe7e2007-05-16 19:47:19 +0000574 if (t->isFunctionType()) // C99 6.3.2.1p4
Chris Lattnerea3c20b2007-06-02 22:47:04 +0000575 return Context.getPointerType(t);
Steve Naroffa78fe7e2007-05-16 19:47:19 +0000576 if (const ArrayType *ary = dyn_cast<ArrayType>(t.getCanonicalType()))
577 return Context.getPointerType(ary->getElementType()); // C99 6.3.2.1p3
Steve Naroff1926c832007-04-24 00:23:05 +0000578 return t;
579}
580
Steve Naroff71b59a92007-06-04 22:22:31 +0000581/// UsualUnaryConversion - Performs various conversions that are common to most
582/// operators (C99 6.3). The conversions of array and function types are
583/// sometimes surpressed. For example, the array->pointer conversion doesn't
584/// apply if the array is an argument to the sizeof or address (&) operators.
585/// In these instances, this routine should *not* be called.
Steve Naroff7a5af782007-07-13 16:58:59 +0000586QualType Sema::UsualUnaryConversions(Expr *&expr) {
587 QualType t = expr->getType();
Steve Naroff71b59a92007-06-04 22:22:31 +0000588 assert(!t.isNull() && "UsualUnaryConversions - missing type");
589
Steve Naroffb8ea4fb2007-07-13 23:32:42 +0000590 if (t->isPromotableIntegerType()) { // C99 6.3.1.1p2
591 // expr = new ImplicitCastExpr(Context.IntTy, expr);
Steve Naroff71b59a92007-06-04 22:22:31 +0000592 return Context.IntTy;
Steve Naroffb8ea4fb2007-07-13 23:32:42 +0000593 }
594 return DefaultFunctionArrayConversion(expr);
Steve Naroff71b59a92007-06-04 22:22:31 +0000595}
596
Steve Naroff1926c832007-04-24 00:23:05 +0000597/// UsualArithmeticConversions - Performs various conversions that are common to
598/// binary operators (C99 6.3.1.8). If both operands aren't arithmetic, this
599/// routine returns the first non-arithmetic type found. The client is
600/// responsible for emitting appropriate error diagnostics.
Steve Naroffb8ea4fb2007-07-13 23:32:42 +0000601QualType Sema::UsualArithmeticConversions(Expr *&lhsExpr, Expr *&rhsExpr,
602 QualType &lhs, QualType &rhs) {
603 lhs = UsualUnaryConversions(lhsExpr);
604 rhs = UsualUnaryConversions(rhsExpr);
Steve Naroff1926c832007-04-24 00:23:05 +0000605
Chris Lattner0c46c5d2007-06-02 23:53:17 +0000606 // If both types are identical, no conversion is needed.
607 if (lhs == rhs)
608 return lhs;
609
610 // If either side is a non-arithmetic type (e.g. a pointer), we are done.
611 // The caller can deal with this (e.g. pointer + int).
Steve Naroffb01bbe32007-04-25 01:22:31 +0000612 if (!lhs->isArithmeticType())
613 return lhs;
Steve Narofff633d092007-04-25 19:01:39 +0000614 if (!rhs->isArithmeticType())
Steve Naroffb01bbe32007-04-25 01:22:31 +0000615 return rhs;
Steve Naroff1926c832007-04-24 00:23:05 +0000616
Chris Lattner0c46c5d2007-06-02 23:53:17 +0000617 // At this point, we have two different arithmetic types.
Steve Naroffb01bbe32007-04-25 01:22:31 +0000618
619 // Handle complex types first (C99 6.3.1.8p1).
620 if (lhs->isComplexType() || rhs->isComplexType()) {
621 // if we have an integer operand, the result is the complex type.
622 if (rhs->isIntegerType())
623 return lhs;
624 if (lhs->isIntegerType())
625 return rhs;
626
Steve Naroffe4718892007-04-27 18:30:00 +0000627 return Context.maxComplexType(lhs, rhs);
Steve Naroffbf223ba2007-04-24 20:56:26 +0000628 }
Chris Lattner0c46c5d2007-06-02 23:53:17 +0000629
Steve Naroffb01bbe32007-04-25 01:22:31 +0000630 // Now handle "real" floating types (i.e. float, double, long double).
631 if (lhs->isRealFloatingType() || rhs->isRealFloatingType()) {
632 // if we have an integer operand, the result is the real floating type.
633 if (rhs->isIntegerType())
634 return lhs;
635 if (lhs->isIntegerType())
636 return rhs;
637
638 // we have two real floating types, float/complex combos were handled above.
Steve Naroffe4718892007-04-27 18:30:00 +0000639 return Context.maxFloatingType(lhs, rhs);
Steve Naroffb01bbe32007-04-25 01:22:31 +0000640 }
Steve Naroffe4718892007-04-27 18:30:00 +0000641 return Context.maxIntegerType(lhs, rhs);
Steve Narofff1e53692007-03-23 22:27:02 +0000642}
643
Steve Naroff3f597292007-05-11 22:18:03 +0000644// CheckPointerTypesForAssignment - This is a very tricky routine (despite
645// being closely modeled after the C99 spec:-). The odd characteristic of this
646// routine is it effectively iqnores the qualifiers on the top level pointee.
647// This circumvents the usual type rules specified in 6.2.7p1 & 6.7.5.[1-3].
648// FIXME: add a couple examples in this comment.
Steve Naroff98cf3e92007-06-06 18:38:38 +0000649Sema::AssignmentCheckResult
650Sema::CheckPointerTypesForAssignment(QualType lhsType, QualType rhsType) {
Steve Naroff3f597292007-05-11 22:18:03 +0000651 QualType lhptee, rhptee;
Steve Naroff1f4d7272007-05-11 04:00:31 +0000652
653 // get the "pointed to" type (ignoring qualifiers at the top level)
Steve Naroff3f597292007-05-11 22:18:03 +0000654 lhptee = cast<PointerType>(lhsType.getCanonicalType())->getPointeeType();
655 rhptee = cast<PointerType>(rhsType.getCanonicalType())->getPointeeType();
Steve Naroff1f4d7272007-05-11 04:00:31 +0000656
657 // make sure we operate on the canonical type
Steve Naroff3f597292007-05-11 22:18:03 +0000658 lhptee = lhptee.getCanonicalType();
659 rhptee = rhptee.getCanonicalType();
Steve Naroff1f4d7272007-05-11 04:00:31 +0000660
Steve Naroff98cf3e92007-06-06 18:38:38 +0000661 AssignmentCheckResult r = Compatible;
662
Steve Naroff3f597292007-05-11 22:18:03 +0000663 // C99 6.5.16.1p1: This following citation is common to constraints
664 // 3 & 4 (below). ...and the type *pointed to* by the left has all the
665 // qualifiers of the type *pointed to* by the right;
666 if ((lhptee.getQualifiers() & rhptee.getQualifiers()) !=
667 rhptee.getQualifiers())
668 r = CompatiblePointerDiscardsQualifiers;
669
670 // C99 6.5.16.1p1 (constraint 4): If one operand is a pointer to an object or
671 // incomplete type and the other is a pointer to a qualified or unqualified
672 // version of void...
673 if (lhptee.getUnqualifiedType()->isVoidType() &&
674 (rhptee->isObjectType() || rhptee->isIncompleteType()))
675 ;
676 else if (rhptee.getUnqualifiedType()->isVoidType() &&
677 (lhptee->isObjectType() || lhptee->isIncompleteType()))
678 ;
679 // C99 6.5.16.1p1 (constraint 3): both operands are pointers to qualified or
680 // unqualified versions of compatible types, ...
681 else if (!Type::typesAreCompatible(lhptee.getUnqualifiedType(),
682 rhptee.getUnqualifiedType()))
683 r = IncompatiblePointer; // this "trumps" PointerAssignDiscardsQualifiers
Steve Naroff98cf3e92007-06-06 18:38:38 +0000684 return r;
Steve Naroff1f4d7272007-05-11 04:00:31 +0000685}
686
Steve Naroff98cf3e92007-06-06 18:38:38 +0000687/// CheckAssignmentConstraints (C99 6.5.16) - This routine currently
Steve Naroff17f76e02007-05-03 21:03:48 +0000688/// has code to accommodate several GCC extensions when type checking
689/// pointers. Here are some objectionable examples that GCC considers warnings:
690///
691/// int a, *pint;
692/// short *pshort;
693/// struct foo *pfoo;
694///
695/// pint = pshort; // warning: assignment from incompatible pointer type
696/// a = pint; // warning: assignment makes integer from pointer without a cast
697/// pint = a; // warning: assignment makes pointer from integer without a cast
698/// pint = pfoo; // warning: assignment from incompatible pointer type
699///
700/// As a result, the code for dealing with pointers is more complex than the
701/// C99 spec dictates.
702/// Note: the warning above turn into errors when -pedantic-errors is enabled.
703///
Steve Naroff98cf3e92007-06-06 18:38:38 +0000704Sema::AssignmentCheckResult
705Sema::CheckAssignmentConstraints(QualType lhsType, QualType rhsType) {
Steve Naroff84ff4b42007-07-09 21:31:10 +0000706 if (lhsType->isArithmeticType() && rhsType->isArithmeticType()) {
Steve Naroffe728ba32007-07-10 22:20:04 +0000707 if (lhsType->isVectorType() || rhsType->isVectorType()) {
708 if (lhsType.getCanonicalType() != rhsType.getCanonicalType())
709 return Incompatible;
710 }
Steve Naroff98cf3e92007-06-06 18:38:38 +0000711 return Compatible;
Steve Naroff84ff4b42007-07-09 21:31:10 +0000712 } else if (lhsType->isPointerType()) {
Steve Naroff98cf3e92007-06-06 18:38:38 +0000713 if (rhsType->isIntegerType())
714 return PointerFromInt;
715
Steve Naroff3f597292007-05-11 22:18:03 +0000716 if (rhsType->isPointerType())
Steve Naroff98cf3e92007-06-06 18:38:38 +0000717 return CheckPointerTypesForAssignment(lhsType, rhsType);
Steve Naroff9eb24652007-05-02 21:58:15 +0000718 } else if (rhsType->isPointerType()) {
Steve Naroff98cf3e92007-06-06 18:38:38 +0000719 // C99 6.5.16.1p1: the left operand is _Bool and the right is a pointer.
720 if ((lhsType->isIntegerType()) && (lhsType != Context.BoolTy))
721 return IntFromPointer;
722
Steve Naroff3f597292007-05-11 22:18:03 +0000723 if (lhsType->isPointerType())
Steve Naroff98cf3e92007-06-06 18:38:38 +0000724 return CheckPointerTypesForAssignment(lhsType, rhsType);
Steve Naroff1f4d7272007-05-11 04:00:31 +0000725 } else if (isa<TagType>(lhsType) && isa<TagType>(rhsType)) {
726 if (Type::tagTypesAreCompatible(lhsType, rhsType))
Steve Naroff98cf3e92007-06-06 18:38:38 +0000727 return Compatible;
Bill Wendlingdb4f06e2007-06-02 23:29:59 +0000728 } else if (lhsType->isReferenceType() || rhsType->isReferenceType()) {
729 if (Type::referenceTypesAreCompatible(lhsType, rhsType))
Steve Naroff98cf3e92007-06-06 18:38:38 +0000730 return Compatible;
Bill Wendling216423b2007-05-30 06:30:29 +0000731 }
Steve Naroff98cf3e92007-06-06 18:38:38 +0000732 return Incompatible;
Steve Naroff9eb24652007-05-02 21:58:15 +0000733}
734
Steve Naroffb8ea4fb2007-07-13 23:32:42 +0000735Sema::AssignmentCheckResult
736Sema::CheckSingleAssignmentConstraints(QualType lhsType, Expr *&rExpr) {
737 // This check seems unnatural, however it is necessary to insure the proper
738 // conversion of functions/arrays. If the conversion were done for all
739 // DeclExpr's (created by ParseIdentifierExpr), it would mess up the unary
740 // expressions that surpress this implicit conversion (&, sizeof).
741 QualType rhsType = DefaultFunctionArrayConversion(rExpr);
742
743 return CheckAssignmentConstraints(lhsType, rhsType);
744}
745
746Sema::AssignmentCheckResult
747Sema::CheckCompoundAssignmentConstraints(QualType lhsType, QualType rhsType) {
748 return CheckAssignmentConstraints(lhsType, rhsType);
749}
750
Steve Naroff7a5af782007-07-13 16:58:59 +0000751inline void Sema::InvalidOperands(SourceLocation loc, Expr *&lex, Expr *&rex) {
Steve Naroff6f49f5d2007-05-29 14:23:36 +0000752 Diag(loc, diag::err_typecheck_invalid_operands,
753 lex->getType().getAsString(), rex->getType().getAsString(),
754 lex->getSourceRange(), rex->getSourceRange());
755}
756
Steve Naroff7a5af782007-07-13 16:58:59 +0000757inline QualType Sema::CheckVectorOperands(SourceLocation loc, Expr *&lex,
758 Expr *&rex) {
Steve Naroff84ff4b42007-07-09 21:31:10 +0000759 QualType lhsType = lex->getType(), rhsType = rex->getType();
760
761 // make sure the vector types are identical.
762 if (lhsType == rhsType)
763 return lhsType;
764 // You cannot convert between vector values of different size.
765 Diag(loc, diag::err_typecheck_vector_not_convertable,
766 lex->getType().getAsString(), rex->getType().getAsString(),
767 lex->getSourceRange(), rex->getSourceRange());
768 return QualType();
769}
770
Steve Naroff218bc2b2007-05-04 21:54:46 +0000771inline QualType Sema::CheckMultiplyDivideOperands(
Steve Naroff7a5af782007-07-13 16:58:59 +0000772 Expr *&lex, Expr *&rex, SourceLocation loc)
Steve Naroff5c10d4b2007-04-20 23:42:24 +0000773{
Steve Naroffb8ea4fb2007-07-13 23:32:42 +0000774 QualType lhsType = lex->getType(), rhsType = rex->getType();
775
776 if (lhsType->isVectorType() || rhsType->isVectorType())
Steve Naroff84ff4b42007-07-09 21:31:10 +0000777 return CheckVectorOperands(loc, lex, rex);
Steve Naroff7a5af782007-07-13 16:58:59 +0000778
Steve Naroffb8ea4fb2007-07-13 23:32:42 +0000779 QualType resType = UsualArithmeticConversions(lex, rex, lhsType, rhsType);
Steve Naroff5c10d4b2007-04-20 23:42:24 +0000780
Steve Naroff218bc2b2007-05-04 21:54:46 +0000781 if (resType->isArithmeticType())
782 return resType;
Steve Naroff6f49f5d2007-05-29 14:23:36 +0000783 InvalidOperands(loc, lex, rex);
Steve Naroff218bc2b2007-05-04 21:54:46 +0000784 return QualType();
Steve Naroff26c8ea52007-03-21 21:08:52 +0000785}
786
Steve Naroff218bc2b2007-05-04 21:54:46 +0000787inline QualType Sema::CheckRemainderOperands(
Steve Naroff7a5af782007-07-13 16:58:59 +0000788 Expr *&lex, Expr *&rex, SourceLocation loc)
Steve Naroff218bc2b2007-05-04 21:54:46 +0000789{
Steve Naroffb8ea4fb2007-07-13 23:32:42 +0000790 QualType lhsType = lex->getType(), rhsType = rex->getType();
791
792 QualType resType = UsualArithmeticConversions(lex, rex, lhsType, rhsType);
Steve Naroff218bc2b2007-05-04 21:54:46 +0000793
794 if (resType->isIntegerType())
795 return resType;
Steve Naroff6f49f5d2007-05-29 14:23:36 +0000796 InvalidOperands(loc, lex, rex);
Steve Naroff218bc2b2007-05-04 21:54:46 +0000797 return QualType();
798}
799
800inline QualType Sema::CheckAdditionOperands( // C99 6.5.6
Steve Naroff7a5af782007-07-13 16:58:59 +0000801 Expr *&lex, Expr *&rex, SourceLocation loc)
Steve Naroff1926c832007-04-24 00:23:05 +0000802{
Steve Naroffe4718892007-04-27 18:30:00 +0000803 QualType lhsType = lex->getType(), rhsType = rex->getType();
Steve Naroff84ff4b42007-07-09 21:31:10 +0000804
805 if (lhsType->isVectorType() || rhsType->isVectorType())
Steve Naroff7a5af782007-07-13 16:58:59 +0000806 return CheckVectorOperands(loc, lex, rex);
807
Steve Naroffb8ea4fb2007-07-13 23:32:42 +0000808 QualType resType = UsualArithmeticConversions(lex, rex, lhsType, rhsType);
Steve Naroff4ae0ac62007-07-06 23:09:18 +0000809
Steve Naroffe4718892007-04-27 18:30:00 +0000810 // handle the common case first (both operands are arithmetic).
811 if (resType->isArithmeticType())
Steve Naroff218bc2b2007-05-04 21:54:46 +0000812 return resType;
813
814 if ((lhsType->isPointerType() && rhsType->isIntegerType()) ||
815 (lhsType->isIntegerType() && rhsType->isPointerType()))
816 return resType;
Steve Naroff6f49f5d2007-05-29 14:23:36 +0000817 InvalidOperands(loc, lex, rex);
Steve Naroff218bc2b2007-05-04 21:54:46 +0000818 return QualType();
Steve Naroff26c8ea52007-03-21 21:08:52 +0000819}
820
Steve Naroff218bc2b2007-05-04 21:54:46 +0000821inline QualType Sema::CheckSubtractionOperands( // C99 6.5.6
Steve Naroff7a5af782007-07-13 16:58:59 +0000822 Expr *&lex, Expr *&rex, SourceLocation loc)
Steve Naroff218bc2b2007-05-04 21:54:46 +0000823{
824 QualType lhsType = lex->getType(), rhsType = rex->getType();
Steve Naroff84ff4b42007-07-09 21:31:10 +0000825
826 if (lhsType->isVectorType() || rhsType->isVectorType())
827 return CheckVectorOperands(loc, lex, rex);
Steve Naroffb8ea4fb2007-07-13 23:32:42 +0000828
829 QualType resType = UsualArithmeticConversions(lex, rex, lhsType, rhsType);
Steve Naroff218bc2b2007-05-04 21:54:46 +0000830
831 // handle the common case first (both operands are arithmetic).
832 if (resType->isArithmeticType())
833 return resType;
Chris Lattnerd2b88ab2007-07-13 03:05:23 +0000834 if (lhsType->isPointerType() && rhsType->isIntegerType())
Steve Naroff218bc2b2007-05-04 21:54:46 +0000835 return resType;
Chris Lattnerd2b88ab2007-07-13 03:05:23 +0000836 if (lhsType->isPointerType() && rhsType->isPointerType())
837 return Context.getPointerDiffType();
Steve Naroff6f49f5d2007-05-29 14:23:36 +0000838 InvalidOperands(loc, lex, rex);
Steve Naroff218bc2b2007-05-04 21:54:46 +0000839 return QualType();
840}
841
842inline QualType Sema::CheckShiftOperands( // C99 6.5.7
Steve Naroff7a5af782007-07-13 16:58:59 +0000843 Expr *&lex, Expr *&rex, SourceLocation loc)
Steve Naroff1926c832007-04-24 00:23:05 +0000844{
Chris Lattner1d411a82007-06-05 20:52:21 +0000845 // FIXME: Shifts don't perform usual arithmetic conversions. This is wrong
846 // for int << longlong -> the result type should be int, not long long.
Steve Naroffd6fbee82007-06-13 15:42:33 +0000847 QualType lhsType = lex->getType(), rhsType = rex->getType();
Steve Naroffb8ea4fb2007-07-13 23:32:42 +0000848 QualType resType = UsualArithmeticConversions(lex, rex, lhsType, rhsType);
Steve Naroff1926c832007-04-24 00:23:05 +0000849
Steve Naroff218bc2b2007-05-04 21:54:46 +0000850 if (resType->isIntegerType())
851 return resType;
Steve Naroff6f49f5d2007-05-29 14:23:36 +0000852 InvalidOperands(loc, lex, rex);
Steve Naroff218bc2b2007-05-04 21:54:46 +0000853 return QualType();
Steve Naroff26c8ea52007-03-21 21:08:52 +0000854}
855
Steve Naroff218bc2b2007-05-04 21:54:46 +0000856inline QualType Sema::CheckRelationalOperands( // C99 6.5.8
Steve Naroff7a5af782007-07-13 16:58:59 +0000857 Expr *&lex, Expr *&rex, SourceLocation loc)
Steve Naroff1926c832007-04-24 00:23:05 +0000858{
Steve Naroff7a5af782007-07-13 16:58:59 +0000859 QualType lType = UsualUnaryConversions(lex);
860 QualType rType = UsualUnaryConversions(rex);
Steve Naroff1926c832007-04-24 00:23:05 +0000861
862 if (lType->isRealType() && rType->isRealType())
Steve Naroff218bc2b2007-05-04 21:54:46 +0000863 return Context.IntTy;
Steve Naroffe4718892007-04-27 18:30:00 +0000864
Steve Naroff75c17232007-06-13 21:41:08 +0000865 if (lType->isPointerType()) {
866 if (rType->isPointerType())
867 return Context.IntTy;
868 if (rType->isIntegerType()) {
869 if (!rex->isNullPointerConstant())
870 Diag(loc, diag::ext_typecheck_comparison_of_pointer_integer,
871 lex->getSourceRange(), rex->getSourceRange());
872 return Context.IntTy; // the previous diagnostic is a GCC extension.
873 }
874 } else if (rType->isPointerType()) {
875 if (lType->isIntegerType()) {
876 if (!lex->isNullPointerConstant())
877 Diag(loc, diag::ext_typecheck_comparison_of_pointer_integer,
878 lex->getSourceRange(), rex->getSourceRange());
879 return Context.IntTy; // the previous diagnostic is a GCC extension.
880 }
Steve Naroff043d45d2007-05-15 02:32:35 +0000881 }
Steve Naroff6f49f5d2007-05-29 14:23:36 +0000882 InvalidOperands(loc, lex, rex);
Steve Naroff218bc2b2007-05-04 21:54:46 +0000883 return QualType();
Steve Naroff26c8ea52007-03-21 21:08:52 +0000884}
885
Steve Naroff218bc2b2007-05-04 21:54:46 +0000886inline QualType Sema::CheckEqualityOperands( // C99 6.5.9
Steve Naroff7a5af782007-07-13 16:58:59 +0000887 Expr *&lex, Expr *&rex, SourceLocation loc)
Steve Naroff1926c832007-04-24 00:23:05 +0000888{
Steve Naroff7a5af782007-07-13 16:58:59 +0000889 QualType lType = UsualUnaryConversions(lex);
890 QualType rType = UsualUnaryConversions(rex);
Steve Naroff1926c832007-04-24 00:23:05 +0000891
892 if (lType->isArithmeticType() && rType->isArithmeticType())
Steve Naroff218bc2b2007-05-04 21:54:46 +0000893 return Context.IntTy;
Steve Naroff218bc2b2007-05-04 21:54:46 +0000894
Steve Naroff75c17232007-06-13 21:41:08 +0000895 if (lType->isPointerType()) {
896 if (rType->isPointerType())
897 return Context.IntTy;
898 if (rType->isIntegerType()) {
899 if (!rex->isNullPointerConstant())
900 Diag(loc, diag::ext_typecheck_comparison_of_pointer_integer,
901 lex->getSourceRange(), rex->getSourceRange());
902 return Context.IntTy; // the previous diagnostic is a GCC extension.
903 }
904 } else if (rType->isPointerType()) {
905 if (lType->isIntegerType()) {
906 if (!lex->isNullPointerConstant())
907 Diag(loc, diag::ext_typecheck_comparison_of_pointer_integer,
908 lex->getSourceRange(), rex->getSourceRange());
909 return Context.IntTy; // the previous diagnostic is a GCC extension.
910 }
Steve Naroff043d45d2007-05-15 02:32:35 +0000911 }
Steve Naroff6f49f5d2007-05-29 14:23:36 +0000912 InvalidOperands(loc, lex, rex);
Steve Naroff218bc2b2007-05-04 21:54:46 +0000913 return QualType();
Steve Naroff26c8ea52007-03-21 21:08:52 +0000914}
915
Steve Naroff218bc2b2007-05-04 21:54:46 +0000916inline QualType Sema::CheckBitwiseOperands(
Steve Naroff7a5af782007-07-13 16:58:59 +0000917 Expr *&lex, Expr *&rex, SourceLocation loc)
Steve Naroff1926c832007-04-24 00:23:05 +0000918{
Steve Naroffd6fbee82007-06-13 15:42:33 +0000919 QualType lhsType = lex->getType(), rhsType = rex->getType();
Steve Naroff84ff4b42007-07-09 21:31:10 +0000920
921 if (lhsType->isVectorType() || rhsType->isVectorType())
922 return CheckVectorOperands(loc, lex, rex);
Steve Naroffb8ea4fb2007-07-13 23:32:42 +0000923
924 QualType resType = UsualArithmeticConversions(lex, rex, lhsType, rhsType);
Steve Naroff1926c832007-04-24 00:23:05 +0000925
Steve Naroff218bc2b2007-05-04 21:54:46 +0000926 if (resType->isIntegerType())
927 return resType;
Steve Naroff6f49f5d2007-05-29 14:23:36 +0000928 InvalidOperands(loc, lex, rex);
Steve Naroff218bc2b2007-05-04 21:54:46 +0000929 return QualType();
Steve Naroff26c8ea52007-03-21 21:08:52 +0000930}
931
Steve Naroff218bc2b2007-05-04 21:54:46 +0000932inline QualType Sema::CheckLogicalOperands( // C99 6.5.[13,14]
Steve Naroff7a5af782007-07-13 16:58:59 +0000933 Expr *&lex, Expr *&rex, SourceLocation loc)
Steve Naroff1926c832007-04-24 00:23:05 +0000934{
Steve Naroff7a5af782007-07-13 16:58:59 +0000935 QualType lhsType = UsualUnaryConversions(lex);
936 QualType rhsType = UsualUnaryConversions(rex);
Steve Naroffe4718892007-04-27 18:30:00 +0000937
Steve Naroff218bc2b2007-05-04 21:54:46 +0000938 if (lhsType->isScalarType() || rhsType->isScalarType())
939 return Context.IntTy;
Steve Naroff6f49f5d2007-05-29 14:23:36 +0000940 InvalidOperands(loc, lex, rex);
Steve Naroff218bc2b2007-05-04 21:54:46 +0000941 return QualType();
Steve Naroffae4143e2007-04-26 20:39:23 +0000942}
943
Steve Naroff35d85152007-05-07 00:24:15 +0000944inline QualType Sema::CheckAssignmentOperands( // C99 6.5.16.1
945 Expr *lex, Expr *rex, SourceLocation loc, QualType compoundType)
Steve Naroffae4143e2007-04-26 20:39:23 +0000946{
Steve Naroff0af91202007-04-27 21:51:21 +0000947 QualType lhsType = lex->getType();
Steve Naroff35d85152007-05-07 00:24:15 +0000948 QualType rhsType = compoundType.isNull() ? rex->getType() : compoundType;
Steve Naroff1f4d7272007-05-11 04:00:31 +0000949 bool hadError = false;
Steve Naroff9358c712007-05-27 23:58:33 +0000950 Expr::isModifiableLvalueResult mlval = lex->isModifiableLvalue();
951
952 switch (mlval) { // C99 6.5.16p2
953 case Expr::MLV_Valid:
954 break;
955 case Expr::MLV_ConstQualified:
956 Diag(loc, diag::err_typecheck_assign_const, lex->getSourceRange());
957 hadError = true;
958 break;
959 case Expr::MLV_ArrayType:
960 Diag(loc, diag::err_typecheck_array_not_modifiable_lvalue,
961 lhsType.getAsString(), lex->getSourceRange());
962 return QualType();
963 case Expr::MLV_NotObjectType:
964 Diag(loc, diag::err_typecheck_non_object_not_modifiable_lvalue,
965 lhsType.getAsString(), lex->getSourceRange());
966 return QualType();
967 case Expr::MLV_InvalidExpression:
968 Diag(loc, diag::err_typecheck_expression_not_modifiable_lvalue,
969 lex->getSourceRange());
970 return QualType();
971 case Expr::MLV_IncompleteType:
972 case Expr::MLV_IncompleteVoidType:
973 Diag(loc, diag::err_typecheck_incomplete_type_not_modifiable_lvalue,
974 lhsType.getAsString(), lex->getSourceRange());
975 return QualType();
Steve Naroff218bc2b2007-05-04 21:54:46 +0000976 }
977 if (lhsType == rhsType) // common case, fast path...
978 return lhsType;
979
Steve Naroffb8ea4fb2007-07-13 23:32:42 +0000980 AssignmentCheckResult result;
981
982 if (compoundType.isNull())
983 result = CheckSingleAssignmentConstraints(lhsType, rex);
984 else
985 result = CheckCompoundAssignmentConstraints(lhsType, rhsType);
986
Steve Naroff218bc2b2007-05-04 21:54:46 +0000987 // decode the result (notice that extensions still return a type).
988 switch (result) {
989 case Compatible:
Steve Naroff1f4d7272007-05-11 04:00:31 +0000990 break;
Steve Naroff218bc2b2007-05-04 21:54:46 +0000991 case Incompatible:
Steve Naroffe845e272007-05-18 01:06:45 +0000992 Diag(loc, diag::err_typecheck_assign_incompatible,
Chris Lattner84e160a2007-05-19 07:03:17 +0000993 lhsType.getAsString(), rhsType.getAsString(),
994 lex->getSourceRange(), rex->getSourceRange());
Steve Naroff1f4d7272007-05-11 04:00:31 +0000995 hadError = true;
996 break;
Steve Naroff218bc2b2007-05-04 21:54:46 +0000997 case PointerFromInt:
998 // check for null pointer constant (C99 6.3.2.3p3)
Steve Naroff9992bba2007-05-30 16:27:15 +0000999 if (compoundType.isNull() && !rex->isNullPointerConstant()) {
Steve Naroff56faab22007-05-30 04:20:12 +00001000 Diag(loc, diag::ext_typecheck_assign_pointer_int,
1001 lhsType.getAsString(), rhsType.getAsString(),
Steve Naroff9358c712007-05-27 23:58:33 +00001002 lex->getSourceRange(), rex->getSourceRange());
Steve Naroff9992bba2007-05-30 16:27:15 +00001003 }
Steve Naroff1f4d7272007-05-11 04:00:31 +00001004 break;
Steve Naroff56faab22007-05-30 04:20:12 +00001005 case IntFromPointer:
1006 Diag(loc, diag::ext_typecheck_assign_pointer_int,
1007 lhsType.getAsString(), rhsType.getAsString(),
Steve Naroff9358c712007-05-27 23:58:33 +00001008 lex->getSourceRange(), rex->getSourceRange());
Steve Naroff1f4d7272007-05-11 04:00:31 +00001009 break;
Steve Naroff218bc2b2007-05-04 21:54:46 +00001010 case IncompatiblePointer:
Steve Naroff9358c712007-05-27 23:58:33 +00001011 Diag(loc, diag::ext_typecheck_assign_incompatible_pointer,
1012 lhsType.getAsString(), rhsType.getAsString(),
1013 lex->getSourceRange(), rex->getSourceRange());
Steve Naroff1f4d7272007-05-11 04:00:31 +00001014 break;
1015 case CompatiblePointerDiscardsQualifiers:
Steve Naroff9358c712007-05-27 23:58:33 +00001016 Diag(loc, diag::ext_typecheck_assign_discards_qualifiers,
1017 lhsType.getAsString(), rhsType.getAsString(),
1018 lex->getSourceRange(), rex->getSourceRange());
Steve Naroff1f4d7272007-05-11 04:00:31 +00001019 break;
Steve Naroff218bc2b2007-05-04 21:54:46 +00001020 }
Steve Naroff98cf3e92007-06-06 18:38:38 +00001021 // C99 6.5.16p3: The type of an assignment expression is the type of the
1022 // left operand unless the left operand has qualified type, in which case
1023 // it is the unqualified version of the type of the left operand.
1024 // C99 6.5.16.1p2: In simple assignment, the value of the right operand
1025 // is converted to the type of the assignment expression (above).
1026 // C++ 5.17p1: the type of the assignment expression is that of its left oprdu.
1027 return hadError ? QualType() : lhsType.getUnqualifiedType();
Steve Naroffae4143e2007-04-26 20:39:23 +00001028}
1029
Steve Naroff218bc2b2007-05-04 21:54:46 +00001030inline QualType Sema::CheckCommaOperands( // C99 6.5.17
Steve Naroff7a5af782007-07-13 16:58:59 +00001031 Expr *&lex, Expr *&rex, SourceLocation loc) {
1032 return UsualUnaryConversions(rex);
Steve Naroff95af0132007-03-30 23:47:58 +00001033}
1034
Steve Naroff7a5af782007-07-13 16:58:59 +00001035/// CheckIncrementDecrementOperand - unlike most "Check" methods, this routine
1036/// doesn't need to call UsualUnaryConversions or UsualArithmeticConversions.
Steve Naroff71ce2e02007-05-18 22:53:50 +00001037QualType Sema::CheckIncrementDecrementOperand(Expr *op, SourceLocation OpLoc) {
Steve Naroff7a5af782007-07-13 16:58:59 +00001038 QualType resType = op->getType();
Steve Naroff35d85152007-05-07 00:24:15 +00001039 assert(!resType.isNull() && "no type for increment/decrement expression");
Steve Naroffd50c88e2007-04-05 21:15:20 +00001040
Steve Naroff46ba1eb2007-04-03 23:13:13 +00001041 // C99 6.5.2.4p1
Steve Naroff35d85152007-05-07 00:24:15 +00001042 if (const PointerType *pt = dyn_cast<PointerType>(resType)) {
1043 if (!pt->getPointeeType()->isObjectType()) { // C99 6.5.2.4p2, 6.5.6p2
Steve Naroff71ce2e02007-05-18 22:53:50 +00001044 Diag(OpLoc, diag::err_typecheck_arithmetic_incomplete_type,
1045 resType.getAsString(), op->getSourceRange());
Steve Naroff35d85152007-05-07 00:24:15 +00001046 return QualType();
1047 }
1048 } else if (!resType->isRealType()) {
Steve Naroff95af0132007-03-30 23:47:58 +00001049 // FIXME: Allow Complex as a GCC extension.
Steve Naroff71ce2e02007-05-18 22:53:50 +00001050 Diag(OpLoc, diag::err_typecheck_illegal_increment_decrement,
1051 resType.getAsString(), op->getSourceRange());
Steve Naroff35d85152007-05-07 00:24:15 +00001052 return QualType();
Steve Naroff46ba1eb2007-04-03 23:13:13 +00001053 }
Steve Naroff094046f2007-05-13 03:21:25 +00001054 // At this point, we know we have a real or pointer type. Now make sure
1055 // the operand is a modifiable lvalue.
Steve Naroff9358c712007-05-27 23:58:33 +00001056 Expr::isModifiableLvalueResult mlval = op->isModifiableLvalue();
1057 if (mlval != Expr::MLV_Valid) {
1058 // FIXME: emit a more precise diagnostic...
Steve Naroff71ce2e02007-05-18 22:53:50 +00001059 Diag(OpLoc, diag::err_typecheck_invalid_lvalue_incr_decr,
Chris Lattner84e160a2007-05-19 07:03:17 +00001060 op->getSourceRange());
Steve Naroff35d85152007-05-07 00:24:15 +00001061 return QualType();
1062 }
1063 return resType;
Steve Naroff26c8ea52007-03-21 21:08:52 +00001064}
1065
Steve Naroff1926c832007-04-24 00:23:05 +00001066/// getPrimaryDeclaration - Helper function for CheckAddressOfOperand().
Steve Naroff47500512007-04-19 23:00:49 +00001067/// This routine allows us to typecheck complex/recursive expressions
1068/// where the declaration is needed for type checking. Here are some
1069/// examples: &s.xx, &s.zz[1].yy, &(1+2), &(XX), &"123"[2].
Steve Naroff1926c832007-04-24 00:23:05 +00001070static Decl *getPrimaryDeclaration(Expr *e) {
Steve Naroff47500512007-04-19 23:00:49 +00001071 switch (e->getStmtClass()) {
1072 case Stmt::DeclRefExprClass:
1073 return cast<DeclRefExpr>(e)->getDecl();
1074 case Stmt::MemberExprClass:
Steve Naroff1926c832007-04-24 00:23:05 +00001075 return getPrimaryDeclaration(cast<MemberExpr>(e)->getBase());
Steve Naroff47500512007-04-19 23:00:49 +00001076 case Stmt::ArraySubscriptExprClass:
Steve Naroff1926c832007-04-24 00:23:05 +00001077 return getPrimaryDeclaration(cast<ArraySubscriptExpr>(e)->getBase());
Steve Naroff47500512007-04-19 23:00:49 +00001078 case Stmt::CallExprClass:
Steve Naroff1926c832007-04-24 00:23:05 +00001079 return getPrimaryDeclaration(cast<CallExpr>(e)->getCallee());
Steve Naroff47500512007-04-19 23:00:49 +00001080 case Stmt::UnaryOperatorClass:
Steve Naroff1926c832007-04-24 00:23:05 +00001081 return getPrimaryDeclaration(cast<UnaryOperator>(e)->getSubExpr());
Steve Naroff47500512007-04-19 23:00:49 +00001082 case Stmt::ParenExprClass:
Steve Naroff1926c832007-04-24 00:23:05 +00001083 return getPrimaryDeclaration(cast<ParenExpr>(e)->getSubExpr());
Steve Naroff47500512007-04-19 23:00:49 +00001084 default:
1085 return 0;
1086 }
1087}
1088
1089/// CheckAddressOfOperand - The operand of & must be either a function
1090/// designator or an lvalue designating an object. If it is an lvalue, the
1091/// object cannot be declared with storage class register or be a bit field.
1092/// Note: The usual conversions are *not* applied to the operand of the &
Steve Naroffa78fe7e2007-05-16 19:47:19 +00001093/// operator (C99 6.3.2.1p[2-4]), and its result is never an lvalue.
Steve Naroff35d85152007-05-07 00:24:15 +00001094QualType Sema::CheckAddressOfOperand(Expr *op, SourceLocation OpLoc) {
Steve Naroff1926c832007-04-24 00:23:05 +00001095 Decl *dcl = getPrimaryDeclaration(op);
Steve Naroff9358c712007-05-27 23:58:33 +00001096 Expr::isLvalueResult lval = op->isLvalue();
Steve Naroff47500512007-04-19 23:00:49 +00001097
Steve Naroff9358c712007-05-27 23:58:33 +00001098 if (lval != Expr::LV_Valid) { // C99 6.5.3.2p1
Steve Naroff5dd642e2007-05-14 18:14:51 +00001099 if (dcl && isa<FunctionDecl>(dcl)) // allow function designators
1100 ;
Steve Naroff9358c712007-05-27 23:58:33 +00001101 else { // FIXME: emit more specific diag...
Steve Naroff71ce2e02007-05-18 22:53:50 +00001102 Diag(OpLoc, diag::err_typecheck_invalid_lvalue_addrof,
1103 op->getSourceRange());
Steve Naroff35d85152007-05-07 00:24:15 +00001104 return QualType();
1105 }
Steve Naroff47500512007-04-19 23:00:49 +00001106 } else if (dcl) {
1107 // We have an lvalue with a decl. Make sure the decl is not declared
1108 // with the register storage-class specifier.
1109 if (const VarDecl *vd = dyn_cast<VarDecl>(dcl)) {
Steve Naroff35d85152007-05-07 00:24:15 +00001110 if (vd->getStorageClass() == VarDecl::Register) {
Steve Naroff71ce2e02007-05-18 22:53:50 +00001111 Diag(OpLoc, diag::err_typecheck_address_of_register,
Chris Lattner84e160a2007-05-19 07:03:17 +00001112 op->getSourceRange());
Steve Naroff35d85152007-05-07 00:24:15 +00001113 return QualType();
1114 }
Steve Narofff633d092007-04-25 19:01:39 +00001115 } else
1116 assert(0 && "Unknown/unexpected decl type");
1117
Steve Naroff47500512007-04-19 23:00:49 +00001118 // FIXME: add check for bitfields!
1119 }
1120 // If the operand has type "type", the result has type "pointer to type".
Steve Naroff35d85152007-05-07 00:24:15 +00001121 return Context.getPointerType(op->getType());
Steve Naroff47500512007-04-19 23:00:49 +00001122}
1123
Steve Naroff35d85152007-05-07 00:24:15 +00001124QualType Sema::CheckIndirectionOperand(Expr *op, SourceLocation OpLoc) {
Steve Naroff7a5af782007-07-13 16:58:59 +00001125 QualType qType = UsualUnaryConversions(op);
Steve Naroff35d85152007-05-07 00:24:15 +00001126
Steve Naroff47500512007-04-19 23:00:49 +00001127 assert(!qType.isNull() && "no type for * expression");
1128
Steve Naroff758ada12007-05-28 16:15:57 +00001129 if (PointerType *PT = dyn_cast<PointerType>(qType.getCanonicalType())) {
1130 QualType ptype = PT->getPointeeType();
1131 // C99 6.5.3.2p4. "if it points to an object,...".
1132 if (ptype->isIncompleteType()) { // An incomplete type is not an object
1133 // GCC compat: special case 'void *' (treat as warning).
1134 if (ptype->isVoidType()) {
1135 Diag(OpLoc, diag::ext_typecheck_deref_ptr_to_void,
Steve Naroffeb9da942007-05-30 00:06:37 +00001136 qType.getAsString(), op->getSourceRange());
Steve Naroff758ada12007-05-28 16:15:57 +00001137 } else {
1138 Diag(OpLoc, diag::err_typecheck_deref_incomplete_type,
Steve Naroffeb9da942007-05-30 00:06:37 +00001139 ptype.getAsString(), op->getSourceRange());
Steve Naroff758ada12007-05-28 16:15:57 +00001140 return QualType();
1141 }
1142 }
1143 return ptype;
1144 }
1145 Diag(OpLoc, diag::err_typecheck_indirection_requires_pointer,
Steve Naroffeb9da942007-05-30 00:06:37 +00001146 qType.getAsString(), op->getSourceRange());
Steve Naroff35d85152007-05-07 00:24:15 +00001147 return QualType();
Steve Naroff1926c832007-04-24 00:23:05 +00001148}
Steve Naroff218bc2b2007-05-04 21:54:46 +00001149
1150static inline BinaryOperator::Opcode ConvertTokenKindToBinaryOpcode(
1151 tok::TokenKind Kind) {
1152 BinaryOperator::Opcode Opc;
1153 switch (Kind) {
1154 default: assert(0 && "Unknown binop!");
1155 case tok::star: Opc = BinaryOperator::Mul; break;
1156 case tok::slash: Opc = BinaryOperator::Div; break;
1157 case tok::percent: Opc = BinaryOperator::Rem; break;
1158 case tok::plus: Opc = BinaryOperator::Add; break;
1159 case tok::minus: Opc = BinaryOperator::Sub; break;
1160 case tok::lessless: Opc = BinaryOperator::Shl; break;
1161 case tok::greatergreater: Opc = BinaryOperator::Shr; break;
1162 case tok::lessequal: Opc = BinaryOperator::LE; break;
1163 case tok::less: Opc = BinaryOperator::LT; break;
1164 case tok::greaterequal: Opc = BinaryOperator::GE; break;
1165 case tok::greater: Opc = BinaryOperator::GT; break;
1166 case tok::exclaimequal: Opc = BinaryOperator::NE; break;
1167 case tok::equalequal: Opc = BinaryOperator::EQ; break;
1168 case tok::amp: Opc = BinaryOperator::And; break;
1169 case tok::caret: Opc = BinaryOperator::Xor; break;
1170 case tok::pipe: Opc = BinaryOperator::Or; break;
1171 case tok::ampamp: Opc = BinaryOperator::LAnd; break;
1172 case tok::pipepipe: Opc = BinaryOperator::LOr; break;
1173 case tok::equal: Opc = BinaryOperator::Assign; break;
1174 case tok::starequal: Opc = BinaryOperator::MulAssign; break;
1175 case tok::slashequal: Opc = BinaryOperator::DivAssign; break;
1176 case tok::percentequal: Opc = BinaryOperator::RemAssign; break;
1177 case tok::plusequal: Opc = BinaryOperator::AddAssign; break;
1178 case tok::minusequal: Opc = BinaryOperator::SubAssign; break;
1179 case tok::lesslessequal: Opc = BinaryOperator::ShlAssign; break;
1180 case tok::greatergreaterequal: Opc = BinaryOperator::ShrAssign; break;
1181 case tok::ampequal: Opc = BinaryOperator::AndAssign; break;
1182 case tok::caretequal: Opc = BinaryOperator::XorAssign; break;
1183 case tok::pipeequal: Opc = BinaryOperator::OrAssign; break;
1184 case tok::comma: Opc = BinaryOperator::Comma; break;
1185 }
1186 return Opc;
1187}
1188
Steve Naroff35d85152007-05-07 00:24:15 +00001189static inline UnaryOperator::Opcode ConvertTokenKindToUnaryOpcode(
1190 tok::TokenKind Kind) {
1191 UnaryOperator::Opcode Opc;
1192 switch (Kind) {
1193 default: assert(0 && "Unknown unary op!");
1194 case tok::plusplus: Opc = UnaryOperator::PreInc; break;
1195 case tok::minusminus: Opc = UnaryOperator::PreDec; break;
1196 case tok::amp: Opc = UnaryOperator::AddrOf; break;
1197 case tok::star: Opc = UnaryOperator::Deref; break;
1198 case tok::plus: Opc = UnaryOperator::Plus; break;
1199 case tok::minus: Opc = UnaryOperator::Minus; break;
1200 case tok::tilde: Opc = UnaryOperator::Not; break;
1201 case tok::exclaim: Opc = UnaryOperator::LNot; break;
1202 case tok::kw_sizeof: Opc = UnaryOperator::SizeOf; break;
1203 case tok::kw___alignof: Opc = UnaryOperator::AlignOf; break;
1204 case tok::kw___real: Opc = UnaryOperator::Real; break;
1205 case tok::kw___imag: Opc = UnaryOperator::Imag; break;
Chris Lattnerd0f76512007-06-08 22:16:53 +00001206 case tok::kw___extension__: Opc = UnaryOperator::Extension; break;
Steve Naroff35d85152007-05-07 00:24:15 +00001207 }
1208 return Opc;
1209}
1210
Steve Naroff218bc2b2007-05-04 21:54:46 +00001211// Binary Operators. 'Tok' is the token for the operator.
1212Action::ExprResult Sema::ParseBinOp(SourceLocation TokLoc, tok::TokenKind Kind,
1213 ExprTy *LHS, ExprTy *RHS) {
1214 BinaryOperator::Opcode Opc = ConvertTokenKindToBinaryOpcode(Kind);
1215 Expr *lhs = (Expr *)LHS, *rhs = (Expr*)RHS;
1216
1217 assert((lhs != 0) && "ParseBinOp(): missing left expression");
1218 assert((rhs != 0) && "ParseBinOp(): missing right expression");
1219
Chris Lattner256c21b2007-06-28 03:53:10 +00001220 QualType ResultTy; // Result type of the binary operator.
1221 QualType CompTy; // Computation type for compound assignments (e.g. '+=')
Steve Naroff218bc2b2007-05-04 21:54:46 +00001222
1223 switch (Opc) {
1224 default:
1225 assert(0 && "Unknown binary expr!");
1226 case BinaryOperator::Assign:
Chris Lattner256c21b2007-06-28 03:53:10 +00001227 ResultTy = CheckAssignmentOperands(lhs, rhs, TokLoc, QualType());
Steve Naroff218bc2b2007-05-04 21:54:46 +00001228 break;
1229 case BinaryOperator::Mul:
1230 case BinaryOperator::Div:
Chris Lattner256c21b2007-06-28 03:53:10 +00001231 ResultTy = CheckMultiplyDivideOperands(lhs, rhs, TokLoc);
Steve Naroff218bc2b2007-05-04 21:54:46 +00001232 break;
1233 case BinaryOperator::Rem:
Chris Lattner256c21b2007-06-28 03:53:10 +00001234 ResultTy = CheckRemainderOperands(lhs, rhs, TokLoc);
Steve Naroff218bc2b2007-05-04 21:54:46 +00001235 break;
1236 case BinaryOperator::Add:
Chris Lattner256c21b2007-06-28 03:53:10 +00001237 ResultTy = CheckAdditionOperands(lhs, rhs, TokLoc);
Steve Naroff218bc2b2007-05-04 21:54:46 +00001238 break;
1239 case BinaryOperator::Sub:
Chris Lattner256c21b2007-06-28 03:53:10 +00001240 ResultTy = CheckSubtractionOperands(lhs, rhs, TokLoc);
Steve Naroff218bc2b2007-05-04 21:54:46 +00001241 break;
1242 case BinaryOperator::Shl:
1243 case BinaryOperator::Shr:
Chris Lattner256c21b2007-06-28 03:53:10 +00001244 ResultTy = CheckShiftOperands(lhs, rhs, TokLoc);
Steve Naroff218bc2b2007-05-04 21:54:46 +00001245 break;
1246 case BinaryOperator::LE:
1247 case BinaryOperator::LT:
1248 case BinaryOperator::GE:
1249 case BinaryOperator::GT:
Chris Lattner256c21b2007-06-28 03:53:10 +00001250 ResultTy = CheckRelationalOperands(lhs, rhs, TokLoc);
Steve Naroff218bc2b2007-05-04 21:54:46 +00001251 break;
1252 case BinaryOperator::EQ:
1253 case BinaryOperator::NE:
Chris Lattner256c21b2007-06-28 03:53:10 +00001254 ResultTy = CheckEqualityOperands(lhs, rhs, TokLoc);
Steve Naroff218bc2b2007-05-04 21:54:46 +00001255 break;
1256 case BinaryOperator::And:
1257 case BinaryOperator::Xor:
1258 case BinaryOperator::Or:
Chris Lattner256c21b2007-06-28 03:53:10 +00001259 ResultTy = CheckBitwiseOperands(lhs, rhs, TokLoc);
Steve Naroff218bc2b2007-05-04 21:54:46 +00001260 break;
1261 case BinaryOperator::LAnd:
1262 case BinaryOperator::LOr:
Chris Lattner256c21b2007-06-28 03:53:10 +00001263 ResultTy = CheckLogicalOperands(lhs, rhs, TokLoc);
Steve Naroff218bc2b2007-05-04 21:54:46 +00001264 break;
1265 case BinaryOperator::MulAssign:
1266 case BinaryOperator::DivAssign:
Chris Lattner256c21b2007-06-28 03:53:10 +00001267 CompTy = CheckMultiplyDivideOperands(lhs, rhs, TokLoc);
1268 if (!CompTy.isNull())
1269 ResultTy = CheckAssignmentOperands(lhs, rhs, TokLoc, CompTy);
Steve Naroff218bc2b2007-05-04 21:54:46 +00001270 break;
1271 case BinaryOperator::RemAssign:
Chris Lattner256c21b2007-06-28 03:53:10 +00001272 CompTy = CheckRemainderOperands(lhs, rhs, TokLoc);
1273 if (!CompTy.isNull())
1274 ResultTy = CheckAssignmentOperands(lhs, rhs, TokLoc, CompTy);
Steve Naroff218bc2b2007-05-04 21:54:46 +00001275 break;
1276 case BinaryOperator::AddAssign:
Chris Lattner256c21b2007-06-28 03:53:10 +00001277 CompTy = CheckAdditionOperands(lhs, rhs, TokLoc);
1278 if (!CompTy.isNull())
1279 ResultTy = CheckAssignmentOperands(lhs, rhs, TokLoc, CompTy);
Steve Naroff218bc2b2007-05-04 21:54:46 +00001280 break;
1281 case BinaryOperator::SubAssign:
Chris Lattner256c21b2007-06-28 03:53:10 +00001282 CompTy = CheckSubtractionOperands(lhs, rhs, TokLoc);
1283 if (!CompTy.isNull())
1284 ResultTy = CheckAssignmentOperands(lhs, rhs, TokLoc, CompTy);
Steve Naroff218bc2b2007-05-04 21:54:46 +00001285 break;
1286 case BinaryOperator::ShlAssign:
1287 case BinaryOperator::ShrAssign:
Chris Lattner256c21b2007-06-28 03:53:10 +00001288 CompTy = CheckShiftOperands(lhs, rhs, TokLoc);
1289 if (!CompTy.isNull())
1290 ResultTy = CheckAssignmentOperands(lhs, rhs, TokLoc, CompTy);
Steve Naroff218bc2b2007-05-04 21:54:46 +00001291 break;
1292 case BinaryOperator::AndAssign:
1293 case BinaryOperator::XorAssign:
1294 case BinaryOperator::OrAssign:
Chris Lattner256c21b2007-06-28 03:53:10 +00001295 CompTy = CheckBitwiseOperands(lhs, rhs, TokLoc);
1296 if (!CompTy.isNull())
1297 ResultTy = CheckAssignmentOperands(lhs, rhs, TokLoc, CompTy);
Steve Naroff218bc2b2007-05-04 21:54:46 +00001298 break;
1299 case BinaryOperator::Comma:
Chris Lattner256c21b2007-06-28 03:53:10 +00001300 ResultTy = CheckCommaOperands(lhs, rhs, TokLoc);
Steve Naroff218bc2b2007-05-04 21:54:46 +00001301 break;
1302 }
Chris Lattner256c21b2007-06-28 03:53:10 +00001303 if (ResultTy.isNull())
Steve Naroff218bc2b2007-05-04 21:54:46 +00001304 return true;
Chris Lattner256c21b2007-06-28 03:53:10 +00001305 if (CompTy.isNull())
1306 return new BinaryOperator(lhs, rhs, Opc, ResultTy);
1307 else
Chris Lattner9369a562007-06-29 16:31:29 +00001308 return new CompoundAssignOperator(lhs, rhs, Opc, ResultTy, CompTy);
Steve Naroff218bc2b2007-05-04 21:54:46 +00001309}
1310
Steve Naroff35d85152007-05-07 00:24:15 +00001311// Unary Operators. 'Tok' is the token for the operator.
1312Action::ExprResult Sema::ParseUnaryOp(SourceLocation OpLoc, tok::TokenKind Op,
Chris Lattner86554282007-06-08 22:32:33 +00001313 ExprTy *input) {
1314 Expr *Input = (Expr*)input;
Steve Naroff35d85152007-05-07 00:24:15 +00001315 UnaryOperator::Opcode Opc = ConvertTokenKindToUnaryOpcode(Op);
1316 QualType resultType;
1317 switch (Opc) {
1318 default:
1319 assert(0 && "Unimplemented unary expr!");
1320 case UnaryOperator::PreInc:
1321 case UnaryOperator::PreDec:
Chris Lattner86554282007-06-08 22:32:33 +00001322 resultType = CheckIncrementDecrementOperand(Input, OpLoc);
Steve Naroff35d85152007-05-07 00:24:15 +00001323 break;
1324 case UnaryOperator::AddrOf:
Chris Lattner86554282007-06-08 22:32:33 +00001325 resultType = CheckAddressOfOperand(Input, OpLoc);
Steve Naroff35d85152007-05-07 00:24:15 +00001326 break;
1327 case UnaryOperator::Deref:
Chris Lattner86554282007-06-08 22:32:33 +00001328 resultType = CheckIndirectionOperand(Input, OpLoc);
Steve Naroff35d85152007-05-07 00:24:15 +00001329 break;
1330 case UnaryOperator::Plus:
1331 case UnaryOperator::Minus:
Steve Naroff7a5af782007-07-13 16:58:59 +00001332 resultType = UsualUnaryConversions(Input);
Steve Naroff35d85152007-05-07 00:24:15 +00001333 if (!resultType->isArithmeticType()) // C99 6.5.3.3p1
Chris Lattnerc04bd6a2007-05-16 18:09:54 +00001334 return Diag(OpLoc, diag::err_typecheck_unary_expr,
1335 resultType.getAsString());
Steve Naroff35d85152007-05-07 00:24:15 +00001336 break;
1337 case UnaryOperator::Not: // bitwise complement
Steve Naroff7a5af782007-07-13 16:58:59 +00001338 resultType = UsualUnaryConversions(Input);
Steve Naroff1c8b7d32007-07-12 21:46:55 +00001339 if (!resultType->isIntegerType()) // C99 6.5.3.3p1
1340 return Diag(OpLoc, diag::err_typecheck_unary_expr,
1341 resultType.getAsString());
Steve Naroff35d85152007-05-07 00:24:15 +00001342 break;
1343 case UnaryOperator::LNot: // logical negation
Steve Naroff71b59a92007-06-04 22:22:31 +00001344 // Unlike +/-/~, integer promotions aren't done here (C99 6.5.3.3p5).
Steve Naroffb8ea4fb2007-07-13 23:32:42 +00001345 resultType = DefaultFunctionArrayConversion(Input);
Steve Naroff35d85152007-05-07 00:24:15 +00001346 if (!resultType->isScalarType()) // C99 6.5.3.3p1
Chris Lattnerc04bd6a2007-05-16 18:09:54 +00001347 return Diag(OpLoc, diag::err_typecheck_unary_expr,
1348 resultType.getAsString());
Chris Lattnerbe31ed82007-06-02 19:11:33 +00001349 // LNot always has type int. C99 6.5.3.3p5.
1350 resultType = Context.IntTy;
Steve Naroff35d85152007-05-07 00:24:15 +00001351 break;
1352 case UnaryOperator::SizeOf:
Chris Lattner86554282007-06-08 22:32:33 +00001353 resultType = CheckSizeOfAlignOfOperand(Input->getType(), OpLoc, true);
Steve Naroff043d45d2007-05-15 02:32:35 +00001354 break;
Steve Naroff35d85152007-05-07 00:24:15 +00001355 case UnaryOperator::AlignOf:
Chris Lattner86554282007-06-08 22:32:33 +00001356 resultType = CheckSizeOfAlignOfOperand(Input->getType(), OpLoc, false);
1357 break;
1358 case UnaryOperator::Extension:
1359 // FIXME: does __extension__ cause any promotions? I would think not.
1360 resultType = Input->getType();
Steve Naroff043d45d2007-05-15 02:32:35 +00001361 break;
Steve Naroff35d85152007-05-07 00:24:15 +00001362 }
1363 if (resultType.isNull())
1364 return true;
Chris Lattner86554282007-06-08 22:32:33 +00001365 return new UnaryOperator(Input, Opc, resultType, OpLoc);
Steve Naroff35d85152007-05-07 00:24:15 +00001366}
Chris Lattnereefa10e2007-05-28 06:56:27 +00001367
1368/// ParseAddrLabel - Parse the GNU address of label extension: "&&foo".
1369Sema::ExprResult Sema::ParseAddrLabel(SourceLocation OpLoc,
1370 SourceLocation LabLoc,
1371 IdentifierInfo *LabelII) {
1372 // Look up the record for this label identifier.
1373 LabelStmt *&LabelDecl = LabelMap[LabelII];
1374
1375 // If we haven't seen this label yet, create a forward reference.
1376 if (LabelDecl == 0)
1377 LabelDecl = new LabelStmt(LabLoc, LabelII, 0);
1378
1379 // Create the AST node. The address of a label always has type 'void*'.
1380 return new AddrLabel(OpLoc, LabLoc, LabelDecl,
1381 Context.getPointerType(Context.VoidTy));
1382}
1383