blob: 72e2c44a81ba2d335c0204ab8a93f3dc2e97c6fb [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
118 unsigned IntSize = Context.Target.getIntWidth(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;
144 assert(Context.getIntegerBitwidth(t, Tok.getLocation()) ==
145 ResultVal.getBitWidth() && "long long is not intmax_t?");
Steve Naroff09ef4742007-03-09 23:16:33 +0000146 } else {
Chris Lattner67ca9252007-05-21 01:08:44 +0000147 // If this value fits into a ULL, try to figure out what else it fits into
148 // according to the rules of C99 6.4.4.1p5.
149
150 // Octal, Hexadecimal, and integers with a U suffix are allowed to
151 // be an unsigned int.
152 bool AllowUnsigned = Literal.isUnsigned || Literal.getRadix() != 10;
153
154 // Check from smallest to largest, picking the smallest type we can.
155 if (!Literal.isLong) { // Are int/unsigned possibilities?
156 unsigned IntSize = Context.Target.getIntWidth(Tok.getLocation());
157 // Does it fit in a unsigned int?
158 if (ResultVal.isIntN(IntSize)) {
159 // Does it fit in a signed int?
160 if (!Literal.isUnsigned && ResultVal[IntSize-1] == 0)
161 t = Context.IntTy;
162 else if (AllowUnsigned)
163 t = Context.UnsignedIntTy;
164 }
165
166 if (!t.isNull())
167 ResultVal.trunc(IntSize);
168 }
169
170 // Are long/unsigned long possibilities?
171 if (t.isNull() && !Literal.isLongLong) {
172 unsigned LongSize = Context.Target.getLongWidth(Tok.getLocation());
173
174 // Does it fit in a unsigned long?
175 if (ResultVal.isIntN(LongSize)) {
176 // Does it fit in a signed long?
177 if (!Literal.isUnsigned && ResultVal[LongSize-1] == 0)
178 t = Context.LongTy;
179 else if (AllowUnsigned)
180 t = Context.UnsignedLongTy;
181 }
182 if (!t.isNull())
183 ResultVal.trunc(LongSize);
184 }
185
186 // Finally, check long long if needed.
187 if (t.isNull()) {
188 unsigned LongLongSize =
189 Context.Target.getLongLongWidth(Tok.getLocation());
190
191 // Does it fit in a unsigned long long?
192 if (ResultVal.isIntN(LongLongSize)) {
193 // Does it fit in a signed long long?
194 if (!Literal.isUnsigned && ResultVal[LongLongSize-1] == 0)
195 t = Context.LongLongTy;
196 else if (AllowUnsigned)
197 t = Context.UnsignedLongLongTy;
198 }
199 }
200
201 // If we still couldn't decide a type, we probably have something that
202 // does not fit in a signed long long, but has no U suffix.
203 if (t.isNull()) {
204 Diag(Tok.getLocation(), diag::warn_integer_too_large_for_signed);
205 t = Context.UnsignedLongLongTy;
206 }
Steve Naroff09ef4742007-03-09 23:16:33 +0000207 }
Chris Lattner67ca9252007-05-21 01:08:44 +0000208
209 return new IntegerLiteral(ResultVal, t, Tok.getLocation());
Steve Naroff09ef4742007-03-09 23:16:33 +0000210 } else if (Literal.isFloatingLiteral()) {
Steve Naroff97b9e912007-07-09 23:53:58 +0000211 // FIXME: handle float values > 32 (including compute the real type...).
212 return new FloatingLiteral(Literal.GetFloatValue(), Context.FloatTy,
213 Tok.getLocation());
Steve Naroff09ef4742007-03-09 23:16:33 +0000214 }
Steve Narofff2fb89e2007-03-13 20:29:44 +0000215 return ExprResult(true);
Chris Lattnere168f762006-11-10 05:29:30 +0000216}
217
218Action::ExprResult Sema::ParseParenExpr(SourceLocation L, SourceLocation R,
219 ExprTy *Val) {
Steve Naroffae4143e2007-04-26 20:39:23 +0000220 Expr *e = (Expr *)Val;
221 assert((e != 0) && "ParseParenExpr() missing expr");
Steve Naroff53f07dc2007-05-17 21:49:33 +0000222 return new ParenExpr(L, R, e);
Chris Lattnere168f762006-11-10 05:29:30 +0000223}
224
Steve Naroff71b59a92007-06-04 22:22:31 +0000225/// The UsualUnaryConversions() function is *not* called by this routine.
Steve Naroffa78fe7e2007-05-16 19:47:19 +0000226/// See C99 6.3.2.1p[2-4] for more details.
Steve Naroff043d45d2007-05-15 02:32:35 +0000227QualType Sema::CheckSizeOfAlignOfOperand(QualType exprType,
228 SourceLocation OpLoc, bool isSizeof) {
229 // C99 6.5.3.4p1:
230 if (isa<FunctionType>(exprType) && isSizeof)
231 // alignof(function) is allowed.
232 Diag(OpLoc, diag::ext_sizeof_function_type);
233 else if (exprType->isVoidType())
234 Diag(OpLoc, diag::ext_sizeof_void_type, isSizeof ? "sizeof" : "__alignof");
235 else if (exprType->isIncompleteType()) {
Steve Naroff043d45d2007-05-15 02:32:35 +0000236 Diag(OpLoc, isSizeof ? diag::err_sizeof_incomplete_type :
Chris Lattner3dc3d772007-05-16 18:07:12 +0000237 diag::err_alignof_incomplete_type,
238 exprType.getAsString());
Steve Naroff043d45d2007-05-15 02:32:35 +0000239 return QualType(); // error
240 }
241 // C99 6.5.3.4p4: the type (an unsigned integer type) is size_t.
242 return Context.getSizeType();
243}
244
Chris Lattnere168f762006-11-10 05:29:30 +0000245Action::ExprResult Sema::
246ParseSizeOfAlignOfTypeExpr(SourceLocation OpLoc, bool isSizeof,
Steve Naroff509fe022007-05-17 01:16:00 +0000247 SourceLocation LPLoc, TypeTy *Ty,
248 SourceLocation RPLoc) {
Chris Lattner0d8b1a12006-11-20 04:34:45 +0000249 // If error parsing type, ignore.
250 if (Ty == 0) return true;
Chris Lattner6531c102007-01-23 22:29:49 +0000251
252 // Verify that this is a valid expression.
Steve Naroffe5aa9be2007-04-05 22:36:20 +0000253 QualType ArgTy = QualType::getFromOpaquePtr(Ty);
Chris Lattner6531c102007-01-23 22:29:49 +0000254
Steve Naroff043d45d2007-05-15 02:32:35 +0000255 QualType resultType = CheckSizeOfAlignOfOperand(ArgTy, OpLoc, isSizeof);
256
257 if (resultType.isNull())
258 return true;
Steve Naroff509fe022007-05-17 01:16:00 +0000259 return new SizeOfAlignOfTypeExpr(isSizeof, ArgTy, resultType, OpLoc, RPLoc);
Chris Lattnere168f762006-11-10 05:29:30 +0000260}
261
262
263Action::ExprResult Sema::ParsePostfixUnaryOp(SourceLocation OpLoc,
264 tok::TokenKind Kind,
265 ExprTy *Input) {
266 UnaryOperator::Opcode Opc;
267 switch (Kind) {
268 default: assert(0 && "Unknown unary op!");
269 case tok::plusplus: Opc = UnaryOperator::PostInc; break;
270 case tok::minusminus: Opc = UnaryOperator::PostDec; break;
271 }
Steve Naroff71ce2e02007-05-18 22:53:50 +0000272 QualType result = CheckIncrementDecrementOperand((Expr *)Input, OpLoc);
Steve Naroff35d85152007-05-07 00:24:15 +0000273 if (result.isNull())
274 return true;
Steve Naroff509fe022007-05-17 01:16:00 +0000275 return new UnaryOperator((Expr *)Input, Opc, result, OpLoc);
Chris Lattnere168f762006-11-10 05:29:30 +0000276}
277
278Action::ExprResult Sema::
279ParseArraySubscriptExpr(ExprTy *Base, SourceLocation LLoc,
280 ExprTy *Idx, SourceLocation RLoc) {
Steve Naroffe5aa9be2007-04-05 22:36:20 +0000281 QualType t1 = ((Expr *)Base)->getType();
282 QualType t2 = ((Expr *)Idx)->getType();
Steve Narofff1e53692007-03-23 22:27:02 +0000283
284 assert(!t1.isNull() && "no type for array base expression");
Steve Naroffc1aadb12007-03-28 21:49:40 +0000285 assert(!t2.isNull() && "no type for array index expression");
Steve Narofff1e53692007-03-23 22:27:02 +0000286
Steve Naroffb3096442007-06-09 03:47:53 +0000287 QualType canonT1 = DefaultFunctionArrayConversion(t1).getCanonicalType();
288 QualType canonT2 = DefaultFunctionArrayConversion(t2).getCanonicalType();
Steve Naroffd50c88e2007-04-05 21:15:20 +0000289
Steve Naroffc1aadb12007-03-28 21:49:40 +0000290 // C99 6.5.2.1p2: the expression e1[e2] is by definition precisely equivalent
291 // to the expression *((e1)+(e2)). This means the array "Base" may actually be
Steve Narofff1e53692007-03-23 22:27:02 +0000292 // in the subscript position. As a result, we need to derive the array base
293 // and index from the expression types.
294
Steve 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;
Steve Naroffb3096442007-06-09 03:47:53 +0000300 baseExpr = static_cast<Expr *>(Base);
301 indexExpr = static_cast<Expr *>(Idx);
302 } else if (isa<PointerType>(canonT2)) { // uncommon
Steve Naroffd50c88e2007-04-05 21:15:20 +0000303 baseType = canonT2;
304 indexType = canonT1;
Steve Naroffb3096442007-06-09 03:47:53 +0000305 baseExpr = static_cast<Expr *>(Idx);
306 indexExpr = static_cast<Expr *>(Base);
307 } else {
Steve Naroff4ae0ac62007-07-06 23:09:18 +0000308 return Diag(static_cast<Expr *>(Base)->getLocStart(),
309 diag::err_typecheck_subscript_value,
310 static_cast<Expr *>(Base)->getSourceRange());
Steve Naroffb3096442007-06-09 03:47:53 +0000311 }
Steve Naroffc1aadb12007-03-28 21:49:40 +0000312 // C99 6.5.2.1p1
Steve Naroffb3096442007-06-09 03:47:53 +0000313 if (!indexType->isIntegerType()) {
314 return Diag(indexExpr->getLocStart(), diag::err_typecheck_subscript,
315 indexExpr->getSourceRange());
316 }
Steve Naroffb29cdd52007-07-10 18:23:31 +0000317 QualType resultType;
318 if (PointerType *ary = dyn_cast<PointerType>(baseType)) {
319 // FIXME: need to deal with const...
320 resultType = ary->getPointeeType();
321 // in practice, the following check catches trying to index a pointer
322 // to a function (e.g. void (*)(int)). Functions are not objects in c99.
323 if (!resultType->isObjectType()) {
324 return Diag(baseExpr->getLocStart(),
325 diag::err_typecheck_subscript_not_object,
326 baseType.getAsString(), baseExpr->getSourceRange());
327 }
328 } else if (VectorType *vec = dyn_cast<VectorType>(baseType))
329 resultType = vec->getElementType();
330
Steve Naroff509fe022007-05-17 01:16:00 +0000331 return new ArraySubscriptExpr((Expr*)Base, (Expr*)Idx, resultType, RLoc);
Chris Lattnere168f762006-11-10 05:29:30 +0000332}
333
334Action::ExprResult Sema::
335ParseMemberReferenceExpr(ExprTy *Base, SourceLocation OpLoc,
336 tok::TokenKind OpKind, SourceLocation MemberLoc,
337 IdentifierInfo &Member) {
Steve Naroffe5aa9be2007-04-05 22:36:20 +0000338 QualType qualifiedType = ((Expr *)Base)->getType();
Steve Naroffca8f7122007-04-01 01:41:35 +0000339
340 assert(!qualifiedType.isNull() && "no type for member expression");
341
Steve Naroffe5aa9be2007-04-05 22:36:20 +0000342 QualType canonType = qualifiedType.getCanonicalType();
Steve Narofff1e53692007-03-23 22:27:02 +0000343
344 if (OpKind == tok::arrow) {
Steve Naroffca8f7122007-04-01 01:41:35 +0000345 if (PointerType *PT = dyn_cast<PointerType>(canonType)) {
346 qualifiedType = PT->getPointeeType();
Steve Naroffd50c88e2007-04-05 21:15:20 +0000347 canonType = qualifiedType.getCanonicalType();
Steve Naroffca8f7122007-04-01 01:41:35 +0000348 } else
Steve Narofff1e53692007-03-23 22:27:02 +0000349 return Diag(OpLoc, diag::err_typecheck_member_reference_arrow);
350 }
Steve Naroff92e30f82007-04-02 22:35:25 +0000351 if (!isa<RecordType>(canonType))
352 return Diag(OpLoc, diag::err_typecheck_member_reference_structUnion);
353
354 // get the struct/union definition from the type.
355 RecordDecl *RD = cast<RecordType>(canonType)->getDecl();
Steve Naroffcc321422007-03-26 23:09:51 +0000356
Steve Naroff92e30f82007-04-02 22:35:25 +0000357 if (canonType->isIncompleteType())
358 return Diag(OpLoc, diag::err_typecheck_incomplete_tag, RD->getName());
Steve Naroffcc321422007-03-26 23:09:51 +0000359
Steve Naroff92e30f82007-04-02 22:35:25 +0000360 FieldDecl *MemberDecl = RD->getMember(&Member);
361 if (!MemberDecl)
362 return Diag(OpLoc, diag::err_typecheck_no_member, Member.getName());
363
Steve Naroff9358c712007-05-27 23:58:33 +0000364 return new MemberExpr((Expr*)Base, OpKind == tok::arrow,
365 MemberDecl, MemberLoc);
Chris Lattnere168f762006-11-10 05:29:30 +0000366}
367
368/// ParseCallExpr - Handle a call to Fn with the specified array of arguments.
369/// This provides the location of the left/right parens and a list of comma
370/// locations.
371Action::ExprResult Sema::
372ParseCallExpr(ExprTy *Fn, SourceLocation LParenLoc,
Steve Naroffb8c289d2007-05-08 22:18:00 +0000373 ExprTy **Args, unsigned NumArgsInCall,
Chris Lattnere168f762006-11-10 05:29:30 +0000374 SourceLocation *CommaLocs, SourceLocation RParenLoc) {
Steve Naroff8563f652007-05-28 19:25:56 +0000375 Expr *funcExpr = (Expr *)Fn;
Steve Naroff8563f652007-05-28 19:25:56 +0000376 assert(funcExpr && "no function call expression");
377
Chris Lattner3343f812007-06-06 05:05:41 +0000378 QualType qType = UsualUnaryConversions(funcExpr->getType());
Steve Naroffae4143e2007-04-26 20:39:23 +0000379 assert(!qType.isNull() && "no type for function call expression");
380
Chris Lattner3d01e4e2007-06-06 05:14:05 +0000381 // C99 6.5.2.2p1 - "The expression that denotes the called function shall have
382 // type pointer to function".
383 const PointerType *PT = dyn_cast<PointerType>(qType);
384 if (PT == 0) PT = dyn_cast<PointerType>(qType.getCanonicalType());
385
386 if (PT == 0)
387 return Diag(funcExpr->getLocStart(), diag::err_typecheck_call_not_function,
388 SourceRange(funcExpr->getLocStart(), RParenLoc));
Chris Lattner3343f812007-06-06 05:05:41 +0000389
390 const FunctionType *funcT = dyn_cast<FunctionType>(PT->getPointeeType());
Chris Lattner3d01e4e2007-06-06 05:14:05 +0000391 if (funcT == 0)
392 funcT = dyn_cast<FunctionType>(PT->getPointeeType().getCanonicalType());
393
394 if (funcT == 0)
395 return Diag(funcExpr->getLocStart(), diag::err_typecheck_call_not_function,
396 SourceRange(funcExpr->getLocStart(), RParenLoc));
Steve Naroffb8c289d2007-05-08 22:18:00 +0000397
Steve Naroff17f76e02007-05-03 21:03:48 +0000398 // If a prototype isn't declared, the parser implicitly defines a func decl
399 QualType resultType = funcT->getResultType();
400
401 if (const FunctionTypeProto *proto = dyn_cast<FunctionTypeProto>(funcT)) {
402 // C99 6.5.2.2p7 - the arguments are implicitly converted, as if by
403 // assignment, to the types of the corresponding parameter, ...
404
405 unsigned NumArgsInProto = proto->getNumArgs();
Steve Naroffb8c289d2007-05-08 22:18:00 +0000406 unsigned NumArgsToCheck = NumArgsInCall;
Steve Naroff17f76e02007-05-03 21:03:48 +0000407
Steve Naroffb8c289d2007-05-08 22:18:00 +0000408 if (NumArgsInCall < NumArgsInProto)
Steve Naroff56faab22007-05-30 04:20:12 +0000409 Diag(RParenLoc, diag::err_typecheck_call_too_few_args,
Steve Naroffeb9da942007-05-30 00:06:37 +0000410 funcExpr->getSourceRange());
Steve Naroffb8c289d2007-05-08 22:18:00 +0000411 else if (NumArgsInCall > NumArgsInProto) {
Steve Naroff8563f652007-05-28 19:25:56 +0000412 if (!proto->isVariadic()) {
Steve Naroff56faab22007-05-30 04:20:12 +0000413 Diag(((Expr **)Args)[NumArgsInProto+1]->getLocStart(),
414 diag::err_typecheck_call_too_many_args, funcExpr->getSourceRange(),
415 ((Expr **)Args)[NumArgsInProto+1]->getSourceRange());
Steve Naroff8563f652007-05-28 19:25:56 +0000416 }
Steve Naroffb8c289d2007-05-08 22:18:00 +0000417 NumArgsToCheck = NumArgsInProto;
Steve Naroff17f76e02007-05-03 21:03:48 +0000418 }
419 // Continue to check argument types (even if we have too few/many args).
Steve Naroffb8c289d2007-05-08 22:18:00 +0000420 for (unsigned i = 0; i < NumArgsToCheck; i++) {
Steve Naroff8563f652007-05-28 19:25:56 +0000421 Expr *argExpr = ((Expr **)Args)[i];
Steve Naroff8563f652007-05-28 19:25:56 +0000422 assert(argExpr && "ParseCallExpr(): missing argument expression");
423
Steve Naroff17f76e02007-05-03 21:03:48 +0000424 QualType lhsType = proto->getArgType(i);
Steve Naroff8563f652007-05-28 19:25:56 +0000425 QualType rhsType = argExpr->getType();
Steve Naroff17f76e02007-05-03 21:03:48 +0000426
427 if (lhsType == rhsType) // common case, fast path...
428 continue;
Steve Naroff98cf3e92007-06-06 18:38:38 +0000429
430 AssignmentCheckResult result = CheckAssignmentConstraints(lhsType,
431 rhsType);
Steve Naroff6f49f5d2007-05-29 14:23:36 +0000432 SourceLocation l = argExpr->getLocStart();
Steve Naroff17f76e02007-05-03 21:03:48 +0000433
434 // decode the result (notice that AST's are still created for extensions).
Steve Naroff17f76e02007-05-03 21:03:48 +0000435 switch (result) {
436 case Compatible:
437 break;
438 case PointerFromInt:
Steve Naroff218bc2b2007-05-04 21:54:46 +0000439 // check for null pointer constant (C99 6.3.2.3p3)
Steve Naroffeb9da942007-05-30 00:06:37 +0000440 if (!argExpr->isNullPointerConstant()) {
Steve Naroff56faab22007-05-30 04:20:12 +0000441 Diag(l, diag::ext_typecheck_passing_pointer_int,
442 lhsType.getAsString(), rhsType.getAsString(),
Steve Naroffeb9da942007-05-30 00:06:37 +0000443 funcExpr->getSourceRange(), argExpr->getSourceRange());
444 }
Steve Naroff17f76e02007-05-03 21:03:48 +0000445 break;
446 case IntFromPointer:
Steve Naroff56faab22007-05-30 04:20:12 +0000447 Diag(l, diag::ext_typecheck_passing_pointer_int,
448 lhsType.getAsString(), rhsType.getAsString(),
Steve Naroffeb9da942007-05-30 00:06:37 +0000449 funcExpr->getSourceRange(), argExpr->getSourceRange());
Steve Naroff17f76e02007-05-03 21:03:48 +0000450 break;
451 case IncompatiblePointer:
Steve Naroff8563f652007-05-28 19:25:56 +0000452 Diag(l, diag::ext_typecheck_passing_incompatible_pointer,
Steve Naroffeb9da942007-05-30 00:06:37 +0000453 rhsType.getAsString(), lhsType.getAsString(),
454 funcExpr->getSourceRange(), argExpr->getSourceRange());
Steve Naroff17f76e02007-05-03 21:03:48 +0000455 break;
Steve Naroff1f4d7272007-05-11 04:00:31 +0000456 case CompatiblePointerDiscardsQualifiers:
Steve Naroffeb9da942007-05-30 00:06:37 +0000457 Diag(l, diag::ext_typecheck_passing_discards_qualifiers,
458 rhsType.getAsString(), lhsType.getAsString(),
459 funcExpr->getSourceRange(), argExpr->getSourceRange());
Steve Naroff1f4d7272007-05-11 04:00:31 +0000460 break;
Steve Naroff17f76e02007-05-03 21:03:48 +0000461 case Incompatible:
Steve Naroff8563f652007-05-28 19:25:56 +0000462 return Diag(l, diag::err_typecheck_passing_incompatible,
463 rhsType.getAsString(), lhsType.getAsString(),
464 funcExpr->getSourceRange(), argExpr->getSourceRange());
Steve Naroff17f76e02007-05-03 21:03:48 +0000465 }
466 }
Steve Naroffb8c289d2007-05-08 22:18:00 +0000467 // Even if the types checked, bail if we had the wrong number of arguments.
468 if ((NumArgsInCall != NumArgsInProto) && !proto->isVariadic())
469 return true;
Steve Naroffae4143e2007-04-26 20:39:23 +0000470 }
Steve Naroff509fe022007-05-17 01:16:00 +0000471 return new CallExpr((Expr*)Fn, (Expr**)Args, NumArgsInCall, resultType,
472 RParenLoc);
Chris Lattnere168f762006-11-10 05:29:30 +0000473}
474
475Action::ExprResult Sema::
476ParseCastExpr(SourceLocation LParenLoc, TypeTy *Ty,
477 SourceLocation RParenLoc, ExprTy *Op) {
Chris Lattner0d8b1a12006-11-20 04:34:45 +0000478 // If error parsing type, ignore.
Steve Naroffae4143e2007-04-26 20:39:23 +0000479 assert((Ty != 0) && "ParseCastExpr(): missing type");
Chris Lattner1d411a82007-06-05 20:52:21 +0000480 // FIXME: Sema for cast is completely missing.
Steve Naroff509fe022007-05-17 01:16:00 +0000481 return new CastExpr(QualType::getFromOpaquePtr(Ty), (Expr*)Op, LParenLoc);
Chris Lattnere168f762006-11-10 05:29:30 +0000482}
483
Steve Narofff8a28c52007-05-15 20:29:32 +0000484inline QualType Sema::CheckConditionalOperands( // C99 6.5.15
485 Expr *Cond, Expr *LHS, Expr *RHS, SourceLocation questionLoc) {
Steve Naroffa78fe7e2007-05-16 19:47:19 +0000486 QualType cond = Cond->getType();
487 QualType lhs = LHS->getType();
488 QualType rhs = RHS->getType();
489
Steve Narofff8a28c52007-05-15 20:29:32 +0000490 assert(!cond.isNull() && "ParseConditionalOp(): no conditional type");
491 assert(!lhs.isNull() && "ParseConditionalOp(): no lhs type");
492 assert(!rhs.isNull() && "ParseConditionalOp(): no rhs type");
493
Steve Naroff71b59a92007-06-04 22:22:31 +0000494 cond = UsualUnaryConversions(cond);
495 lhs = UsualUnaryConversions(lhs);
496 rhs = UsualUnaryConversions(rhs);
Steve Naroffa78fe7e2007-05-16 19:47:19 +0000497
498 // first, check the condition.
499 if (!cond->isScalarType()) { // C99 6.5.15p2
Steve Naroff53f07dc2007-05-17 21:49:33 +0000500 Diag(Cond->getLocStart(), diag::err_typecheck_cond_expect_scalar,
Steve Naroff509fe022007-05-17 01:16:00 +0000501 cond.getAsString());
Steve Naroffa78fe7e2007-05-16 19:47:19 +0000502 return QualType();
503 }
504 // now check the two expressions.
505 if (lhs->isArithmeticType() && rhs->isArithmeticType()) // C99 6.5.15p3,5
506 return UsualArithmeticConversions(lhs, rhs);
507
508 if ((lhs->isStructureType() && rhs->isStructureType()) || // C99 6.5.15p3
509 (lhs->isUnionType() && rhs->isUnionType())) {
510 TagType *lTag = cast<TagType>(lhs.getCanonicalType());
511 TagType *rTag = cast<TagType>(rhs.getCanonicalType());
512
513 if (lTag->getDecl()->getIdentifier() == rTag->getDecl()->getIdentifier())
514 return lhs;
515 else {
516 Diag(questionLoc, diag::err_typecheck_cond_incompatible_operands,
Steve Naroff3c87a572007-05-28 19:40:22 +0000517 lhs.getAsString(), rhs.getAsString(),
518 LHS->getSourceRange(), RHS->getSourceRange());
Steve Naroffa78fe7e2007-05-16 19:47:19 +0000519 return QualType();
520 }
521 }
Steve Naroff30d1fbc2007-05-20 19:46:53 +0000522 if (lhs->isPointerType() && RHS->isNullPointerConstant()) // C99 6.5.15p3
523 return lhs;
524 if (rhs->isPointerType() && LHS->isNullPointerConstant())
525 return rhs;
526
Steve Naroffa78fe7e2007-05-16 19:47:19 +0000527 if (lhs->isPointerType() && rhs->isPointerType()) { // C99 6.5.15p3,6
528 QualType lhptee, rhptee;
529
530 // get the "pointed to" type
531 lhptee = cast<PointerType>(lhs.getCanonicalType())->getPointeeType();
532 rhptee = cast<PointerType>(rhs.getCanonicalType())->getPointeeType();
533
534 // ignore qualifiers on void (C99 6.5.15p3, clause 6)
535 if (lhptee.getUnqualifiedType()->isVoidType() &&
536 (rhptee->isObjectType() || rhptee->isIncompleteType()))
537 return lhs;
538 if (rhptee.getUnqualifiedType()->isVoidType() &&
539 (lhptee->isObjectType() || lhptee->isIncompleteType()))
540 return rhs;
541
542 // FIXME: C99 6.5.15p6: If both operands are pointers to compatible types
543 // *or* to differently qualified versions of compatible types, the result
544 // type is a pointer to an appropriately qualified version of the
545 // *composite* type.
546 if (!Type::typesAreCompatible(lhptee.getUnqualifiedType(),
547 rhptee.getUnqualifiedType())) {
548 Diag(questionLoc, diag::ext_typecheck_cond_incompatible_pointers,
Steve Naroffeb9da942007-05-30 00:06:37 +0000549 lhs.getAsString(), rhs.getAsString(),
550 LHS->getSourceRange(), RHS->getSourceRange());
Steve Naroffa78fe7e2007-05-16 19:47:19 +0000551 return lhs; // FIXME: this is an _ext - is this return o.k?
552 }
553 }
554 if (lhs->isVoidType() && rhs->isVoidType()) // C99 6.5.15p3
555 return lhs;
Steve Naroffa78fe7e2007-05-16 19:47:19 +0000556
557 Diag(questionLoc, diag::err_typecheck_cond_incompatible_operands,
Steve Naroffeb9da942007-05-30 00:06:37 +0000558 lhs.getAsString(), rhs.getAsString(),
559 LHS->getSourceRange(), RHS->getSourceRange());
Steve Naroffa78fe7e2007-05-16 19:47:19 +0000560 return QualType();
Steve Narofff8a28c52007-05-15 20:29:32 +0000561}
562
Chris Lattnere168f762006-11-10 05:29:30 +0000563/// ParseConditionalOp - Parse a ?: operation. Note that 'LHS' may be null
564/// in the case of a the GNU conditional expr extension.
565Action::ExprResult Sema::ParseConditionalOp(SourceLocation QuestionLoc,
566 SourceLocation ColonLoc,
567 ExprTy *Cond, ExprTy *LHS,
568 ExprTy *RHS) {
Steve Narofff8a28c52007-05-15 20:29:32 +0000569 QualType result = CheckConditionalOperands((Expr *)Cond, (Expr *)LHS,
570 (Expr *)RHS, QuestionLoc);
571 if (result.isNull())
572 return true;
573 return new ConditionalOperator((Expr*)Cond, (Expr*)LHS, (Expr*)RHS, result);
Chris Lattnere168f762006-11-10 05:29:30 +0000574}
575
Chris Lattnere3994202007-07-11 21:50:45 +0000576QualType Sema::DefaultFunctionArrayConversion(QualType t) {
Steve Naroffa78fe7e2007-05-16 19:47:19 +0000577 if (t->isFunctionType()) // C99 6.3.2.1p4
Chris Lattnerea3c20b2007-06-02 22:47:04 +0000578 return Context.getPointerType(t);
Steve Naroffa78fe7e2007-05-16 19:47:19 +0000579 if (const ArrayType *ary = dyn_cast<ArrayType>(t.getCanonicalType()))
580 return Context.getPointerType(ary->getElementType()); // C99 6.3.2.1p3
Steve Naroff1926c832007-04-24 00:23:05 +0000581 return t;
582}
583
Steve Naroff71b59a92007-06-04 22:22:31 +0000584/// UsualUnaryConversion - Performs various conversions that are common to most
585/// operators (C99 6.3). The conversions of array and function types are
586/// sometimes surpressed. For example, the array->pointer conversion doesn't
587/// apply if the array is an argument to the sizeof or address (&) operators.
588/// In these instances, this routine should *not* be called.
589QualType Sema::UsualUnaryConversions(QualType t) {
590 assert(!t.isNull() && "UsualUnaryConversions - missing type");
591
592 if (t->isPromotableIntegerType()) // C99 6.3.1.1p2
593 return Context.IntTy;
594 return DefaultFunctionArrayConversion(t);
595}
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 Naroffd6fbee82007-06-13 15:42:33 +0000601QualType Sema::UsualArithmeticConversions(QualType &lhs, QualType &rhs) {
602 lhs = UsualUnaryConversions(lhs);
603 rhs = UsualUnaryConversions(rhs);
Steve Naroff1926c832007-04-24 00:23:05 +0000604
Chris Lattner0c46c5d2007-06-02 23:53:17 +0000605 // If both types are identical, no conversion is needed.
606 if (lhs == rhs)
607 return lhs;
608
609 // If either side is a non-arithmetic type (e.g. a pointer), we are done.
610 // The caller can deal with this (e.g. pointer + int).
Steve Naroffb01bbe32007-04-25 01:22:31 +0000611 if (!lhs->isArithmeticType())
612 return lhs;
Steve Narofff633d092007-04-25 19:01:39 +0000613 if (!rhs->isArithmeticType())
Steve Naroffb01bbe32007-04-25 01:22:31 +0000614 return rhs;
Steve Naroff1926c832007-04-24 00:23:05 +0000615
Chris Lattner0c46c5d2007-06-02 23:53:17 +0000616 // At this point, we have two different arithmetic types.
Steve Naroffb01bbe32007-04-25 01:22:31 +0000617
618 // Handle complex types first (C99 6.3.1.8p1).
619 if (lhs->isComplexType() || rhs->isComplexType()) {
620 // if we have an integer operand, the result is the complex type.
621 if (rhs->isIntegerType())
622 return lhs;
623 if (lhs->isIntegerType())
624 return rhs;
625
Steve Naroffe4718892007-04-27 18:30:00 +0000626 return Context.maxComplexType(lhs, rhs);
Steve Naroffbf223ba2007-04-24 20:56:26 +0000627 }
Chris Lattner0c46c5d2007-06-02 23:53:17 +0000628
Steve Naroffb01bbe32007-04-25 01:22:31 +0000629 // Now handle "real" floating types (i.e. float, double, long double).
630 if (lhs->isRealFloatingType() || rhs->isRealFloatingType()) {
631 // if we have an integer operand, the result is the real floating type.
632 if (rhs->isIntegerType())
633 return lhs;
634 if (lhs->isIntegerType())
635 return rhs;
636
637 // we have two real floating types, float/complex combos were handled above.
Steve Naroffe4718892007-04-27 18:30:00 +0000638 return Context.maxFloatingType(lhs, rhs);
Steve Naroffb01bbe32007-04-25 01:22:31 +0000639 }
Steve Naroffe4718892007-04-27 18:30:00 +0000640 return Context.maxIntegerType(lhs, rhs);
Steve Narofff1e53692007-03-23 22:27:02 +0000641}
642
Steve Naroff3f597292007-05-11 22:18:03 +0000643// CheckPointerTypesForAssignment - This is a very tricky routine (despite
644// being closely modeled after the C99 spec:-). The odd characteristic of this
645// routine is it effectively iqnores the qualifiers on the top level pointee.
646// This circumvents the usual type rules specified in 6.2.7p1 & 6.7.5.[1-3].
647// FIXME: add a couple examples in this comment.
Steve Naroff98cf3e92007-06-06 18:38:38 +0000648Sema::AssignmentCheckResult
649Sema::CheckPointerTypesForAssignment(QualType lhsType, QualType rhsType) {
Steve Naroff3f597292007-05-11 22:18:03 +0000650 QualType lhptee, rhptee;
Steve Naroff1f4d7272007-05-11 04:00:31 +0000651
652 // get the "pointed to" type (ignoring qualifiers at the top level)
Steve Naroff3f597292007-05-11 22:18:03 +0000653 lhptee = cast<PointerType>(lhsType.getCanonicalType())->getPointeeType();
654 rhptee = cast<PointerType>(rhsType.getCanonicalType())->getPointeeType();
Steve Naroff1f4d7272007-05-11 04:00:31 +0000655
656 // make sure we operate on the canonical type
Steve Naroff3f597292007-05-11 22:18:03 +0000657 lhptee = lhptee.getCanonicalType();
658 rhptee = rhptee.getCanonicalType();
Steve Naroff1f4d7272007-05-11 04:00:31 +0000659
Steve Naroff98cf3e92007-06-06 18:38:38 +0000660 AssignmentCheckResult r = Compatible;
661
Steve Naroff3f597292007-05-11 22:18:03 +0000662 // C99 6.5.16.1p1: This following citation is common to constraints
663 // 3 & 4 (below). ...and the type *pointed to* by the left has all the
664 // qualifiers of the type *pointed to* by the right;
665 if ((lhptee.getQualifiers() & rhptee.getQualifiers()) !=
666 rhptee.getQualifiers())
667 r = CompatiblePointerDiscardsQualifiers;
668
669 // C99 6.5.16.1p1 (constraint 4): If one operand is a pointer to an object or
670 // incomplete type and the other is a pointer to a qualified or unqualified
671 // version of void...
672 if (lhptee.getUnqualifiedType()->isVoidType() &&
673 (rhptee->isObjectType() || rhptee->isIncompleteType()))
674 ;
675 else if (rhptee.getUnqualifiedType()->isVoidType() &&
676 (lhptee->isObjectType() || lhptee->isIncompleteType()))
677 ;
678 // C99 6.5.16.1p1 (constraint 3): both operands are pointers to qualified or
679 // unqualified versions of compatible types, ...
680 else if (!Type::typesAreCompatible(lhptee.getUnqualifiedType(),
681 rhptee.getUnqualifiedType()))
682 r = IncompatiblePointer; // this "trumps" PointerAssignDiscardsQualifiers
Steve Naroff98cf3e92007-06-06 18:38:38 +0000683 return r;
Steve Naroff1f4d7272007-05-11 04:00:31 +0000684}
685
Steve Naroff98cf3e92007-06-06 18:38:38 +0000686/// CheckAssignmentConstraints (C99 6.5.16) - This routine currently
Steve Naroff17f76e02007-05-03 21:03:48 +0000687/// has code to accommodate several GCC extensions when type checking
688/// pointers. Here are some objectionable examples that GCC considers warnings:
689///
690/// int a, *pint;
691/// short *pshort;
692/// struct foo *pfoo;
693///
694/// pint = pshort; // warning: assignment from incompatible pointer type
695/// a = pint; // warning: assignment makes integer from pointer without a cast
696/// pint = a; // warning: assignment makes pointer from integer without a cast
697/// pint = pfoo; // warning: assignment from incompatible pointer type
698///
699/// As a result, the code for dealing with pointers is more complex than the
700/// C99 spec dictates.
701/// Note: the warning above turn into errors when -pedantic-errors is enabled.
702///
Steve Naroff98cf3e92007-06-06 18:38:38 +0000703Sema::AssignmentCheckResult
704Sema::CheckAssignmentConstraints(QualType lhsType, QualType rhsType) {
Bill Wendling216423b2007-05-30 06:30:29 +0000705 // This check seems unnatural, however it is necessary to insure the proper
706 // conversion of functions/arrays. If the conversion were done for all
707 // DeclExpr's (created by ParseIdentifierExpr), it would mess up the unary
708 // expressions that surpress this implicit conversion (&, sizeof).
Steve Naroff71b59a92007-06-04 22:22:31 +0000709 rhsType = DefaultFunctionArrayConversion(rhsType);
Steve Naroffb891de32007-05-02 23:51:10 +0000710
Steve Naroff84ff4b42007-07-09 21:31:10 +0000711 if (lhsType->isArithmeticType() && rhsType->isArithmeticType()) {
Steve Naroffe728ba32007-07-10 22:20:04 +0000712 if (lhsType->isVectorType() || rhsType->isVectorType()) {
713 if (lhsType.getCanonicalType() != rhsType.getCanonicalType())
714 return Incompatible;
715 }
Steve Naroff98cf3e92007-06-06 18:38:38 +0000716 return Compatible;
Steve Naroff84ff4b42007-07-09 21:31:10 +0000717 } else if (lhsType->isPointerType()) {
Steve Naroff98cf3e92007-06-06 18:38:38 +0000718 if (rhsType->isIntegerType())
719 return PointerFromInt;
720
Steve Naroff3f597292007-05-11 22:18:03 +0000721 if (rhsType->isPointerType())
Steve Naroff98cf3e92007-06-06 18:38:38 +0000722 return CheckPointerTypesForAssignment(lhsType, rhsType);
Steve Naroff9eb24652007-05-02 21:58:15 +0000723 } else if (rhsType->isPointerType()) {
Steve Naroff98cf3e92007-06-06 18:38:38 +0000724 // C99 6.5.16.1p1: the left operand is _Bool and the right is a pointer.
725 if ((lhsType->isIntegerType()) && (lhsType != Context.BoolTy))
726 return IntFromPointer;
727
Steve Naroff3f597292007-05-11 22:18:03 +0000728 if (lhsType->isPointerType())
Steve Naroff98cf3e92007-06-06 18:38:38 +0000729 return CheckPointerTypesForAssignment(lhsType, rhsType);
Steve Naroff1f4d7272007-05-11 04:00:31 +0000730 } else if (isa<TagType>(lhsType) && isa<TagType>(rhsType)) {
731 if (Type::tagTypesAreCompatible(lhsType, rhsType))
Steve Naroff98cf3e92007-06-06 18:38:38 +0000732 return Compatible;
Bill Wendlingdb4f06e2007-06-02 23:29:59 +0000733 } else if (lhsType->isReferenceType() || rhsType->isReferenceType()) {
734 if (Type::referenceTypesAreCompatible(lhsType, rhsType))
Steve Naroff98cf3e92007-06-06 18:38:38 +0000735 return Compatible;
Bill Wendling216423b2007-05-30 06:30:29 +0000736 }
Steve Naroff98cf3e92007-06-06 18:38:38 +0000737 return Incompatible;
Steve Naroff9eb24652007-05-02 21:58:15 +0000738}
739
Steve Naroff6f49f5d2007-05-29 14:23:36 +0000740inline void Sema::InvalidOperands(SourceLocation loc, Expr *lex, Expr *rex) {
741 Diag(loc, diag::err_typecheck_invalid_operands,
742 lex->getType().getAsString(), rex->getType().getAsString(),
743 lex->getSourceRange(), rex->getSourceRange());
744}
745
Steve Naroff84ff4b42007-07-09 21:31:10 +0000746inline QualType Sema::CheckVectorOperands(SourceLocation loc, Expr *lex,
747 Expr *rex) {
748 QualType lhsType = lex->getType(), rhsType = rex->getType();
749
750 // make sure the vector types are identical.
751 if (lhsType == rhsType)
752 return lhsType;
753 // You cannot convert between vector values of different size.
754 Diag(loc, diag::err_typecheck_vector_not_convertable,
755 lex->getType().getAsString(), rex->getType().getAsString(),
756 lex->getSourceRange(), rex->getSourceRange());
757 return QualType();
758}
759
Steve Naroff218bc2b2007-05-04 21:54:46 +0000760inline QualType Sema::CheckMultiplyDivideOperands(
761 Expr *lex, Expr *rex, SourceLocation loc)
Steve Naroff5c10d4b2007-04-20 23:42:24 +0000762{
Steve Naroffd6fbee82007-06-13 15:42:33 +0000763 QualType lhsType = lex->getType(), rhsType = rex->getType();
Steve Naroff84ff4b42007-07-09 21:31:10 +0000764
765 if (lhsType->isVectorType() || rhsType->isVectorType())
766 return CheckVectorOperands(loc, lex, rex);
Steve Naroffd6fbee82007-06-13 15:42:33 +0000767 QualType resType = UsualArithmeticConversions(lhsType, rhsType);
Steve Naroff5c10d4b2007-04-20 23:42:24 +0000768
Steve Naroff218bc2b2007-05-04 21:54:46 +0000769 if (resType->isArithmeticType())
770 return resType;
Steve Naroff6f49f5d2007-05-29 14:23:36 +0000771 InvalidOperands(loc, lex, rex);
Steve Naroff218bc2b2007-05-04 21:54:46 +0000772 return QualType();
Steve Naroff26c8ea52007-03-21 21:08:52 +0000773}
774
Steve Naroff218bc2b2007-05-04 21:54:46 +0000775inline QualType Sema::CheckRemainderOperands(
776 Expr *lex, Expr *rex, SourceLocation loc)
777{
Steve Naroffd6fbee82007-06-13 15:42:33 +0000778 QualType lhsType = lex->getType(), rhsType = rex->getType();
779 QualType resType = UsualArithmeticConversions(lhsType, rhsType);
Steve Naroff218bc2b2007-05-04 21:54:46 +0000780
781 if (resType->isIntegerType())
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();
785}
786
787inline QualType Sema::CheckAdditionOperands( // C99 6.5.6
788 Expr *lex, Expr *rex, SourceLocation loc)
Steve Naroff1926c832007-04-24 00:23:05 +0000789{
Steve Naroffe4718892007-04-27 18:30:00 +0000790 QualType lhsType = lex->getType(), rhsType = rex->getType();
Steve Naroff84ff4b42007-07-09 21:31:10 +0000791
792 if (lhsType->isVectorType() || rhsType->isVectorType())
793 return CheckVectorOperands(loc, lex, rex);
Steve Naroffe4718892007-04-27 18:30:00 +0000794 QualType resType = UsualArithmeticConversions(lhsType, rhsType);
Steve Naroff4ae0ac62007-07-06 23:09:18 +0000795
Steve Naroffe4718892007-04-27 18:30:00 +0000796 // handle the common case first (both operands are arithmetic).
797 if (resType->isArithmeticType())
Steve Naroff218bc2b2007-05-04 21:54:46 +0000798 return resType;
799
800 if ((lhsType->isPointerType() && rhsType->isIntegerType()) ||
801 (lhsType->isIntegerType() && rhsType->isPointerType()))
802 return resType;
Steve Naroff6f49f5d2007-05-29 14:23:36 +0000803 InvalidOperands(loc, lex, rex);
Steve Naroff218bc2b2007-05-04 21:54:46 +0000804 return QualType();
Steve Naroff26c8ea52007-03-21 21:08:52 +0000805}
806
Steve Naroff218bc2b2007-05-04 21:54:46 +0000807inline QualType Sema::CheckSubtractionOperands( // C99 6.5.6
808 Expr *lex, Expr *rex, SourceLocation loc)
809{
810 QualType lhsType = lex->getType(), rhsType = rex->getType();
Steve Naroff84ff4b42007-07-09 21:31:10 +0000811
812 if (lhsType->isVectorType() || rhsType->isVectorType())
813 return CheckVectorOperands(loc, lex, rex);
Steve Naroff218bc2b2007-05-04 21:54:46 +0000814 QualType resType = UsualArithmeticConversions(lhsType, rhsType);
815
816 // handle the common case first (both operands are arithmetic).
817 if (resType->isArithmeticType())
818 return resType;
819 if ((lhsType->isPointerType() && rhsType->isIntegerType()) ||
820 (lhsType->isPointerType() && rhsType->isPointerType()))
821 return resType;
Steve Naroff6f49f5d2007-05-29 14:23:36 +0000822 InvalidOperands(loc, lex, rex);
Steve Naroff218bc2b2007-05-04 21:54:46 +0000823 return QualType();
824}
825
826inline QualType Sema::CheckShiftOperands( // C99 6.5.7
827 Expr *lex, Expr *rex, SourceLocation loc)
Steve Naroff1926c832007-04-24 00:23:05 +0000828{
Chris Lattner1d411a82007-06-05 20:52:21 +0000829 // FIXME: Shifts don't perform usual arithmetic conversions. This is wrong
830 // for int << longlong -> the result type should be int, not long long.
Steve Naroffd6fbee82007-06-13 15:42:33 +0000831 QualType lhsType = lex->getType(), rhsType = rex->getType();
832 QualType resType = UsualArithmeticConversions(lhsType, rhsType);
Steve Naroff1926c832007-04-24 00:23:05 +0000833
Steve Naroff218bc2b2007-05-04 21:54:46 +0000834 if (resType->isIntegerType())
835 return resType;
Steve Naroff6f49f5d2007-05-29 14:23:36 +0000836 InvalidOperands(loc, lex, rex);
Steve Naroff218bc2b2007-05-04 21:54:46 +0000837 return QualType();
Steve Naroff26c8ea52007-03-21 21:08:52 +0000838}
839
Steve Naroff218bc2b2007-05-04 21:54:46 +0000840inline QualType Sema::CheckRelationalOperands( // C99 6.5.8
841 Expr *lex, Expr *rex, SourceLocation loc)
Steve Naroff1926c832007-04-24 00:23:05 +0000842{
Steve Naroffd6fbee82007-06-13 15:42:33 +0000843 QualType lType = UsualUnaryConversions(lex->getType());
844 QualType rType = UsualUnaryConversions(rex->getType());
Steve Naroff1926c832007-04-24 00:23:05 +0000845
846 if (lType->isRealType() && rType->isRealType())
Steve Naroff218bc2b2007-05-04 21:54:46 +0000847 return Context.IntTy;
Steve Naroffe4718892007-04-27 18:30:00 +0000848
Steve Naroff75c17232007-06-13 21:41:08 +0000849 if (lType->isPointerType()) {
850 if (rType->isPointerType())
851 return Context.IntTy;
852 if (rType->isIntegerType()) {
853 if (!rex->isNullPointerConstant())
854 Diag(loc, diag::ext_typecheck_comparison_of_pointer_integer,
855 lex->getSourceRange(), rex->getSourceRange());
856 return Context.IntTy; // the previous diagnostic is a GCC extension.
857 }
858 } else if (rType->isPointerType()) {
859 if (lType->isIntegerType()) {
860 if (!lex->isNullPointerConstant())
861 Diag(loc, diag::ext_typecheck_comparison_of_pointer_integer,
862 lex->getSourceRange(), rex->getSourceRange());
863 return Context.IntTy; // the previous diagnostic is a GCC extension.
864 }
Steve Naroff043d45d2007-05-15 02:32:35 +0000865 }
Steve Naroff6f49f5d2007-05-29 14:23:36 +0000866 InvalidOperands(loc, lex, rex);
Steve Naroff218bc2b2007-05-04 21:54:46 +0000867 return QualType();
Steve Naroff26c8ea52007-03-21 21:08:52 +0000868}
869
Steve Naroff218bc2b2007-05-04 21:54:46 +0000870inline QualType Sema::CheckEqualityOperands( // C99 6.5.9
871 Expr *lex, Expr *rex, SourceLocation loc)
Steve Naroff1926c832007-04-24 00:23:05 +0000872{
Steve Naroffd6fbee82007-06-13 15:42:33 +0000873 QualType lType = UsualUnaryConversions(lex->getType());
874 QualType rType = UsualUnaryConversions(rex->getType());
Steve Naroff1926c832007-04-24 00:23:05 +0000875
876 if (lType->isArithmeticType() && rType->isArithmeticType())
Steve Naroff218bc2b2007-05-04 21:54:46 +0000877 return Context.IntTy;
Steve Naroff218bc2b2007-05-04 21:54:46 +0000878
Steve Naroff75c17232007-06-13 21:41:08 +0000879 if (lType->isPointerType()) {
880 if (rType->isPointerType())
881 return Context.IntTy;
882 if (rType->isIntegerType()) {
883 if (!rex->isNullPointerConstant())
884 Diag(loc, diag::ext_typecheck_comparison_of_pointer_integer,
885 lex->getSourceRange(), rex->getSourceRange());
886 return Context.IntTy; // the previous diagnostic is a GCC extension.
887 }
888 } else if (rType->isPointerType()) {
889 if (lType->isIntegerType()) {
890 if (!lex->isNullPointerConstant())
891 Diag(loc, diag::ext_typecheck_comparison_of_pointer_integer,
892 lex->getSourceRange(), rex->getSourceRange());
893 return Context.IntTy; // the previous diagnostic is a GCC extension.
894 }
Steve Naroff043d45d2007-05-15 02:32:35 +0000895 }
Steve Naroff6f49f5d2007-05-29 14:23:36 +0000896 InvalidOperands(loc, lex, rex);
Steve Naroff218bc2b2007-05-04 21:54:46 +0000897 return QualType();
Steve Naroff26c8ea52007-03-21 21:08:52 +0000898}
899
Steve Naroff218bc2b2007-05-04 21:54:46 +0000900inline QualType Sema::CheckBitwiseOperands(
901 Expr *lex, Expr *rex, SourceLocation loc)
Steve Naroff1926c832007-04-24 00:23:05 +0000902{
Steve Naroffd6fbee82007-06-13 15:42:33 +0000903 QualType lhsType = lex->getType(), rhsType = rex->getType();
Steve Naroff84ff4b42007-07-09 21:31:10 +0000904
905 if (lhsType->isVectorType() || rhsType->isVectorType())
906 return CheckVectorOperands(loc, lex, rex);
Steve Naroffd6fbee82007-06-13 15:42:33 +0000907 QualType resType = UsualArithmeticConversions(lhsType, rhsType);
Steve Naroff1926c832007-04-24 00:23:05 +0000908
Steve Naroff218bc2b2007-05-04 21:54:46 +0000909 if (resType->isIntegerType())
910 return resType;
Steve Naroff6f49f5d2007-05-29 14:23:36 +0000911 InvalidOperands(loc, lex, rex);
Steve Naroff218bc2b2007-05-04 21:54:46 +0000912 return QualType();
Steve Naroff26c8ea52007-03-21 21:08:52 +0000913}
914
Steve Naroff218bc2b2007-05-04 21:54:46 +0000915inline QualType Sema::CheckLogicalOperands( // C99 6.5.[13,14]
916 Expr *lex, Expr *rex, SourceLocation loc)
Steve Naroff1926c832007-04-24 00:23:05 +0000917{
Steve Naroff71b59a92007-06-04 22:22:31 +0000918 QualType lhsType = UsualUnaryConversions(lex->getType());
919 QualType rhsType = UsualUnaryConversions(rex->getType());
Steve Naroffe4718892007-04-27 18:30:00 +0000920
Steve Naroff218bc2b2007-05-04 21:54:46 +0000921 if (lhsType->isScalarType() || rhsType->isScalarType())
922 return Context.IntTy;
Steve Naroff6f49f5d2007-05-29 14:23:36 +0000923 InvalidOperands(loc, lex, rex);
Steve Naroff218bc2b2007-05-04 21:54:46 +0000924 return QualType();
Steve Naroffae4143e2007-04-26 20:39:23 +0000925}
926
Steve Naroff35d85152007-05-07 00:24:15 +0000927inline QualType Sema::CheckAssignmentOperands( // C99 6.5.16.1
928 Expr *lex, Expr *rex, SourceLocation loc, QualType compoundType)
Steve Naroffae4143e2007-04-26 20:39:23 +0000929{
Steve Naroff0af91202007-04-27 21:51:21 +0000930 QualType lhsType = lex->getType();
Steve Naroff35d85152007-05-07 00:24:15 +0000931 QualType rhsType = compoundType.isNull() ? rex->getType() : compoundType;
Steve Naroff1f4d7272007-05-11 04:00:31 +0000932 bool hadError = false;
Steve Naroff9358c712007-05-27 23:58:33 +0000933 Expr::isModifiableLvalueResult mlval = lex->isModifiableLvalue();
934
935 switch (mlval) { // C99 6.5.16p2
936 case Expr::MLV_Valid:
937 break;
938 case Expr::MLV_ConstQualified:
939 Diag(loc, diag::err_typecheck_assign_const, lex->getSourceRange());
940 hadError = true;
941 break;
942 case Expr::MLV_ArrayType:
943 Diag(loc, diag::err_typecheck_array_not_modifiable_lvalue,
944 lhsType.getAsString(), lex->getSourceRange());
945 return QualType();
946 case Expr::MLV_NotObjectType:
947 Diag(loc, diag::err_typecheck_non_object_not_modifiable_lvalue,
948 lhsType.getAsString(), lex->getSourceRange());
949 return QualType();
950 case Expr::MLV_InvalidExpression:
951 Diag(loc, diag::err_typecheck_expression_not_modifiable_lvalue,
952 lex->getSourceRange());
953 return QualType();
954 case Expr::MLV_IncompleteType:
955 case Expr::MLV_IncompleteVoidType:
956 Diag(loc, diag::err_typecheck_incomplete_type_not_modifiable_lvalue,
957 lhsType.getAsString(), lex->getSourceRange());
958 return QualType();
Steve Naroff218bc2b2007-05-04 21:54:46 +0000959 }
960 if (lhsType == rhsType) // common case, fast path...
961 return lhsType;
962
Steve Naroff98cf3e92007-06-06 18:38:38 +0000963 AssignmentCheckResult result = CheckAssignmentConstraints(lhsType, rhsType);
Steve Naroff218bc2b2007-05-04 21:54:46 +0000964
965 // decode the result (notice that extensions still return a type).
966 switch (result) {
967 case Compatible:
Steve Naroff1f4d7272007-05-11 04:00:31 +0000968 break;
Steve Naroff218bc2b2007-05-04 21:54:46 +0000969 case Incompatible:
Steve Naroffe845e272007-05-18 01:06:45 +0000970 Diag(loc, diag::err_typecheck_assign_incompatible,
Chris Lattner84e160a2007-05-19 07:03:17 +0000971 lhsType.getAsString(), rhsType.getAsString(),
972 lex->getSourceRange(), rex->getSourceRange());
Steve Naroff1f4d7272007-05-11 04:00:31 +0000973 hadError = true;
974 break;
Steve Naroff218bc2b2007-05-04 21:54:46 +0000975 case PointerFromInt:
976 // check for null pointer constant (C99 6.3.2.3p3)
Steve Naroff9992bba2007-05-30 16:27:15 +0000977 if (compoundType.isNull() && !rex->isNullPointerConstant()) {
Steve Naroff56faab22007-05-30 04:20:12 +0000978 Diag(loc, diag::ext_typecheck_assign_pointer_int,
979 lhsType.getAsString(), rhsType.getAsString(),
Steve Naroff9358c712007-05-27 23:58:33 +0000980 lex->getSourceRange(), rex->getSourceRange());
Steve Naroff9992bba2007-05-30 16:27:15 +0000981 }
Steve Naroff1f4d7272007-05-11 04:00:31 +0000982 break;
Steve Naroff56faab22007-05-30 04:20:12 +0000983 case IntFromPointer:
984 Diag(loc, diag::ext_typecheck_assign_pointer_int,
985 lhsType.getAsString(), rhsType.getAsString(),
Steve Naroff9358c712007-05-27 23:58:33 +0000986 lex->getSourceRange(), rex->getSourceRange());
Steve Naroff1f4d7272007-05-11 04:00:31 +0000987 break;
Steve Naroff218bc2b2007-05-04 21:54:46 +0000988 case IncompatiblePointer:
Steve Naroff9358c712007-05-27 23:58:33 +0000989 Diag(loc, diag::ext_typecheck_assign_incompatible_pointer,
990 lhsType.getAsString(), rhsType.getAsString(),
991 lex->getSourceRange(), rex->getSourceRange());
Steve Naroff1f4d7272007-05-11 04:00:31 +0000992 break;
993 case CompatiblePointerDiscardsQualifiers:
Steve Naroff9358c712007-05-27 23:58:33 +0000994 Diag(loc, diag::ext_typecheck_assign_discards_qualifiers,
995 lhsType.getAsString(), rhsType.getAsString(),
996 lex->getSourceRange(), rex->getSourceRange());
Steve Naroff1f4d7272007-05-11 04:00:31 +0000997 break;
Steve Naroff218bc2b2007-05-04 21:54:46 +0000998 }
Steve Naroff98cf3e92007-06-06 18:38:38 +0000999 // C99 6.5.16p3: The type of an assignment expression is the type of the
1000 // left operand unless the left operand has qualified type, in which case
1001 // it is the unqualified version of the type of the left operand.
1002 // C99 6.5.16.1p2: In simple assignment, the value of the right operand
1003 // is converted to the type of the assignment expression (above).
1004 // C++ 5.17p1: the type of the assignment expression is that of its left oprdu.
1005 return hadError ? QualType() : lhsType.getUnqualifiedType();
Steve Naroffae4143e2007-04-26 20:39:23 +00001006}
1007
Steve Naroff218bc2b2007-05-04 21:54:46 +00001008inline QualType Sema::CheckCommaOperands( // C99 6.5.17
Chris Lattner84e160a2007-05-19 07:03:17 +00001009 Expr *lex, Expr *rex, SourceLocation loc) {
Steve Naroff71b59a92007-06-04 22:22:31 +00001010 return UsualUnaryConversions(rex->getType());
Steve Naroff95af0132007-03-30 23:47:58 +00001011}
1012
Steve Naroff71ce2e02007-05-18 22:53:50 +00001013QualType Sema::CheckIncrementDecrementOperand(Expr *op, SourceLocation OpLoc) {
Steve Naroffd6fbee82007-06-13 15:42:33 +00001014 QualType lhsType = op->getType(), rhsType = Context.IntTy;
1015 QualType resType = UsualArithmeticConversions(lhsType, rhsType);
Steve Naroff35d85152007-05-07 00:24:15 +00001016 assert(!resType.isNull() && "no type for increment/decrement expression");
Steve Naroffd50c88e2007-04-05 21:15:20 +00001017
Steve Naroff46ba1eb2007-04-03 23:13:13 +00001018 // C99 6.5.2.4p1
Steve Naroff35d85152007-05-07 00:24:15 +00001019 if (const PointerType *pt = dyn_cast<PointerType>(resType)) {
1020 if (!pt->getPointeeType()->isObjectType()) { // C99 6.5.2.4p2, 6.5.6p2
Steve Naroff71ce2e02007-05-18 22:53:50 +00001021 Diag(OpLoc, diag::err_typecheck_arithmetic_incomplete_type,
1022 resType.getAsString(), op->getSourceRange());
Steve Naroff35d85152007-05-07 00:24:15 +00001023 return QualType();
1024 }
1025 } else if (!resType->isRealType()) {
Steve Naroff95af0132007-03-30 23:47:58 +00001026 // FIXME: Allow Complex as a GCC extension.
Steve Naroff71ce2e02007-05-18 22:53:50 +00001027 Diag(OpLoc, diag::err_typecheck_illegal_increment_decrement,
1028 resType.getAsString(), op->getSourceRange());
Steve Naroff35d85152007-05-07 00:24:15 +00001029 return QualType();
Steve Naroff46ba1eb2007-04-03 23:13:13 +00001030 }
Steve Naroff094046f2007-05-13 03:21:25 +00001031 // At this point, we know we have a real or pointer type. Now make sure
1032 // the operand is a modifiable lvalue.
Steve Naroff9358c712007-05-27 23:58:33 +00001033 Expr::isModifiableLvalueResult mlval = op->isModifiableLvalue();
1034 if (mlval != Expr::MLV_Valid) {
1035 // FIXME: emit a more precise diagnostic...
Steve Naroff71ce2e02007-05-18 22:53:50 +00001036 Diag(OpLoc, diag::err_typecheck_invalid_lvalue_incr_decr,
Chris Lattner84e160a2007-05-19 07:03:17 +00001037 op->getSourceRange());
Steve Naroff35d85152007-05-07 00:24:15 +00001038 return QualType();
1039 }
1040 return resType;
Steve Naroff26c8ea52007-03-21 21:08:52 +00001041}
1042
Steve Naroff1926c832007-04-24 00:23:05 +00001043/// getPrimaryDeclaration - Helper function for CheckAddressOfOperand().
Steve Naroff47500512007-04-19 23:00:49 +00001044/// This routine allows us to typecheck complex/recursive expressions
1045/// where the declaration is needed for type checking. Here are some
1046/// examples: &s.xx, &s.zz[1].yy, &(1+2), &(XX), &"123"[2].
Steve Naroff1926c832007-04-24 00:23:05 +00001047static Decl *getPrimaryDeclaration(Expr *e) {
Steve Naroff47500512007-04-19 23:00:49 +00001048 switch (e->getStmtClass()) {
1049 case Stmt::DeclRefExprClass:
1050 return cast<DeclRefExpr>(e)->getDecl();
1051 case Stmt::MemberExprClass:
Steve Naroff1926c832007-04-24 00:23:05 +00001052 return getPrimaryDeclaration(cast<MemberExpr>(e)->getBase());
Steve Naroff47500512007-04-19 23:00:49 +00001053 case Stmt::ArraySubscriptExprClass:
Steve Naroff1926c832007-04-24 00:23:05 +00001054 return getPrimaryDeclaration(cast<ArraySubscriptExpr>(e)->getBase());
Steve Naroff47500512007-04-19 23:00:49 +00001055 case Stmt::CallExprClass:
Steve Naroff1926c832007-04-24 00:23:05 +00001056 return getPrimaryDeclaration(cast<CallExpr>(e)->getCallee());
Steve Naroff47500512007-04-19 23:00:49 +00001057 case Stmt::UnaryOperatorClass:
Steve Naroff1926c832007-04-24 00:23:05 +00001058 return getPrimaryDeclaration(cast<UnaryOperator>(e)->getSubExpr());
Steve Naroff47500512007-04-19 23:00:49 +00001059 case Stmt::ParenExprClass:
Steve Naroff1926c832007-04-24 00:23:05 +00001060 return getPrimaryDeclaration(cast<ParenExpr>(e)->getSubExpr());
Steve Naroff47500512007-04-19 23:00:49 +00001061 default:
1062 return 0;
1063 }
1064}
1065
1066/// CheckAddressOfOperand - The operand of & must be either a function
1067/// designator or an lvalue designating an object. If it is an lvalue, the
1068/// object cannot be declared with storage class register or be a bit field.
1069/// Note: The usual conversions are *not* applied to the operand of the &
Steve Naroffa78fe7e2007-05-16 19:47:19 +00001070/// operator (C99 6.3.2.1p[2-4]), and its result is never an lvalue.
Steve Naroff35d85152007-05-07 00:24:15 +00001071QualType Sema::CheckAddressOfOperand(Expr *op, SourceLocation OpLoc) {
Steve Naroff1926c832007-04-24 00:23:05 +00001072 Decl *dcl = getPrimaryDeclaration(op);
Steve Naroff9358c712007-05-27 23:58:33 +00001073 Expr::isLvalueResult lval = op->isLvalue();
Steve Naroff47500512007-04-19 23:00:49 +00001074
Steve Naroff9358c712007-05-27 23:58:33 +00001075 if (lval != Expr::LV_Valid) { // C99 6.5.3.2p1
Steve Naroff5dd642e2007-05-14 18:14:51 +00001076 if (dcl && isa<FunctionDecl>(dcl)) // allow function designators
1077 ;
Steve Naroff9358c712007-05-27 23:58:33 +00001078 else { // FIXME: emit more specific diag...
Steve Naroff71ce2e02007-05-18 22:53:50 +00001079 Diag(OpLoc, diag::err_typecheck_invalid_lvalue_addrof,
1080 op->getSourceRange());
Steve Naroff35d85152007-05-07 00:24:15 +00001081 return QualType();
1082 }
Steve Naroff47500512007-04-19 23:00:49 +00001083 } else if (dcl) {
1084 // We have an lvalue with a decl. Make sure the decl is not declared
1085 // with the register storage-class specifier.
1086 if (const VarDecl *vd = dyn_cast<VarDecl>(dcl)) {
Steve Naroff35d85152007-05-07 00:24:15 +00001087 if (vd->getStorageClass() == VarDecl::Register) {
Steve Naroff71ce2e02007-05-18 22:53:50 +00001088 Diag(OpLoc, diag::err_typecheck_address_of_register,
Chris Lattner84e160a2007-05-19 07:03:17 +00001089 op->getSourceRange());
Steve Naroff35d85152007-05-07 00:24:15 +00001090 return QualType();
1091 }
Steve Narofff633d092007-04-25 19:01:39 +00001092 } else
1093 assert(0 && "Unknown/unexpected decl type");
1094
Steve Naroff47500512007-04-19 23:00:49 +00001095 // FIXME: add check for bitfields!
1096 }
1097 // If the operand has type "type", the result has type "pointer to type".
Steve Naroff35d85152007-05-07 00:24:15 +00001098 return Context.getPointerType(op->getType());
Steve Naroff47500512007-04-19 23:00:49 +00001099}
1100
Steve Naroff35d85152007-05-07 00:24:15 +00001101QualType Sema::CheckIndirectionOperand(Expr *op, SourceLocation OpLoc) {
Steve Naroff71b59a92007-06-04 22:22:31 +00001102 QualType qType = UsualUnaryConversions(op->getType());
Steve Naroff35d85152007-05-07 00:24:15 +00001103
Steve Naroff47500512007-04-19 23:00:49 +00001104 assert(!qType.isNull() && "no type for * expression");
1105
Steve Naroff758ada12007-05-28 16:15:57 +00001106 if (PointerType *PT = dyn_cast<PointerType>(qType.getCanonicalType())) {
1107 QualType ptype = PT->getPointeeType();
1108 // C99 6.5.3.2p4. "if it points to an object,...".
1109 if (ptype->isIncompleteType()) { // An incomplete type is not an object
1110 // GCC compat: special case 'void *' (treat as warning).
1111 if (ptype->isVoidType()) {
1112 Diag(OpLoc, diag::ext_typecheck_deref_ptr_to_void,
Steve Naroffeb9da942007-05-30 00:06:37 +00001113 qType.getAsString(), op->getSourceRange());
Steve Naroff758ada12007-05-28 16:15:57 +00001114 } else {
1115 Diag(OpLoc, diag::err_typecheck_deref_incomplete_type,
Steve Naroffeb9da942007-05-30 00:06:37 +00001116 ptype.getAsString(), op->getSourceRange());
Steve Naroff758ada12007-05-28 16:15:57 +00001117 return QualType();
1118 }
1119 }
1120 return ptype;
1121 }
1122 Diag(OpLoc, diag::err_typecheck_indirection_requires_pointer,
Steve Naroffeb9da942007-05-30 00:06:37 +00001123 qType.getAsString(), op->getSourceRange());
Steve Naroff35d85152007-05-07 00:24:15 +00001124 return QualType();
Steve Naroff1926c832007-04-24 00:23:05 +00001125}
Steve Naroff218bc2b2007-05-04 21:54:46 +00001126
1127static inline BinaryOperator::Opcode ConvertTokenKindToBinaryOpcode(
1128 tok::TokenKind Kind) {
1129 BinaryOperator::Opcode Opc;
1130 switch (Kind) {
1131 default: assert(0 && "Unknown binop!");
1132 case tok::star: Opc = BinaryOperator::Mul; break;
1133 case tok::slash: Opc = BinaryOperator::Div; break;
1134 case tok::percent: Opc = BinaryOperator::Rem; break;
1135 case tok::plus: Opc = BinaryOperator::Add; break;
1136 case tok::minus: Opc = BinaryOperator::Sub; break;
1137 case tok::lessless: Opc = BinaryOperator::Shl; break;
1138 case tok::greatergreater: Opc = BinaryOperator::Shr; break;
1139 case tok::lessequal: Opc = BinaryOperator::LE; break;
1140 case tok::less: Opc = BinaryOperator::LT; break;
1141 case tok::greaterequal: Opc = BinaryOperator::GE; break;
1142 case tok::greater: Opc = BinaryOperator::GT; break;
1143 case tok::exclaimequal: Opc = BinaryOperator::NE; break;
1144 case tok::equalequal: Opc = BinaryOperator::EQ; break;
1145 case tok::amp: Opc = BinaryOperator::And; break;
1146 case tok::caret: Opc = BinaryOperator::Xor; break;
1147 case tok::pipe: Opc = BinaryOperator::Or; break;
1148 case tok::ampamp: Opc = BinaryOperator::LAnd; break;
1149 case tok::pipepipe: Opc = BinaryOperator::LOr; break;
1150 case tok::equal: Opc = BinaryOperator::Assign; break;
1151 case tok::starequal: Opc = BinaryOperator::MulAssign; break;
1152 case tok::slashequal: Opc = BinaryOperator::DivAssign; break;
1153 case tok::percentequal: Opc = BinaryOperator::RemAssign; break;
1154 case tok::plusequal: Opc = BinaryOperator::AddAssign; break;
1155 case tok::minusequal: Opc = BinaryOperator::SubAssign; break;
1156 case tok::lesslessequal: Opc = BinaryOperator::ShlAssign; break;
1157 case tok::greatergreaterequal: Opc = BinaryOperator::ShrAssign; break;
1158 case tok::ampequal: Opc = BinaryOperator::AndAssign; break;
1159 case tok::caretequal: Opc = BinaryOperator::XorAssign; break;
1160 case tok::pipeequal: Opc = BinaryOperator::OrAssign; break;
1161 case tok::comma: Opc = BinaryOperator::Comma; break;
1162 }
1163 return Opc;
1164}
1165
Steve Naroff35d85152007-05-07 00:24:15 +00001166static inline UnaryOperator::Opcode ConvertTokenKindToUnaryOpcode(
1167 tok::TokenKind Kind) {
1168 UnaryOperator::Opcode Opc;
1169 switch (Kind) {
1170 default: assert(0 && "Unknown unary op!");
1171 case tok::plusplus: Opc = UnaryOperator::PreInc; break;
1172 case tok::minusminus: Opc = UnaryOperator::PreDec; break;
1173 case tok::amp: Opc = UnaryOperator::AddrOf; break;
1174 case tok::star: Opc = UnaryOperator::Deref; break;
1175 case tok::plus: Opc = UnaryOperator::Plus; break;
1176 case tok::minus: Opc = UnaryOperator::Minus; break;
1177 case tok::tilde: Opc = UnaryOperator::Not; break;
1178 case tok::exclaim: Opc = UnaryOperator::LNot; break;
1179 case tok::kw_sizeof: Opc = UnaryOperator::SizeOf; break;
1180 case tok::kw___alignof: Opc = UnaryOperator::AlignOf; break;
1181 case tok::kw___real: Opc = UnaryOperator::Real; break;
1182 case tok::kw___imag: Opc = UnaryOperator::Imag; break;
Chris Lattnerd0f76512007-06-08 22:16:53 +00001183 case tok::kw___extension__: Opc = UnaryOperator::Extension; break;
Steve Naroff35d85152007-05-07 00:24:15 +00001184 }
1185 return Opc;
1186}
1187
Steve Naroff218bc2b2007-05-04 21:54:46 +00001188// Binary Operators. 'Tok' is the token for the operator.
1189Action::ExprResult Sema::ParseBinOp(SourceLocation TokLoc, tok::TokenKind Kind,
1190 ExprTy *LHS, ExprTy *RHS) {
1191 BinaryOperator::Opcode Opc = ConvertTokenKindToBinaryOpcode(Kind);
1192 Expr *lhs = (Expr *)LHS, *rhs = (Expr*)RHS;
1193
1194 assert((lhs != 0) && "ParseBinOp(): missing left expression");
1195 assert((rhs != 0) && "ParseBinOp(): missing right expression");
1196
Chris Lattner256c21b2007-06-28 03:53:10 +00001197 QualType ResultTy; // Result type of the binary operator.
1198 QualType CompTy; // Computation type for compound assignments (e.g. '+=')
Steve Naroff218bc2b2007-05-04 21:54:46 +00001199
1200 switch (Opc) {
1201 default:
1202 assert(0 && "Unknown binary expr!");
1203 case BinaryOperator::Assign:
Chris Lattner256c21b2007-06-28 03:53:10 +00001204 ResultTy = CheckAssignmentOperands(lhs, rhs, TokLoc, QualType());
Steve Naroff218bc2b2007-05-04 21:54:46 +00001205 break;
1206 case BinaryOperator::Mul:
1207 case BinaryOperator::Div:
Chris Lattner256c21b2007-06-28 03:53:10 +00001208 ResultTy = CheckMultiplyDivideOperands(lhs, rhs, TokLoc);
Steve Naroff218bc2b2007-05-04 21:54:46 +00001209 break;
1210 case BinaryOperator::Rem:
Chris Lattner256c21b2007-06-28 03:53:10 +00001211 ResultTy = CheckRemainderOperands(lhs, rhs, TokLoc);
Steve Naroff218bc2b2007-05-04 21:54:46 +00001212 break;
1213 case BinaryOperator::Add:
Chris Lattner256c21b2007-06-28 03:53:10 +00001214 ResultTy = CheckAdditionOperands(lhs, rhs, TokLoc);
Steve Naroff218bc2b2007-05-04 21:54:46 +00001215 break;
1216 case BinaryOperator::Sub:
Chris Lattner256c21b2007-06-28 03:53:10 +00001217 ResultTy = CheckSubtractionOperands(lhs, rhs, TokLoc);
Steve Naroff218bc2b2007-05-04 21:54:46 +00001218 break;
1219 case BinaryOperator::Shl:
1220 case BinaryOperator::Shr:
Chris Lattner256c21b2007-06-28 03:53:10 +00001221 ResultTy = CheckShiftOperands(lhs, rhs, TokLoc);
Steve Naroff218bc2b2007-05-04 21:54:46 +00001222 break;
1223 case BinaryOperator::LE:
1224 case BinaryOperator::LT:
1225 case BinaryOperator::GE:
1226 case BinaryOperator::GT:
Chris Lattner256c21b2007-06-28 03:53:10 +00001227 ResultTy = CheckRelationalOperands(lhs, rhs, TokLoc);
Steve Naroff218bc2b2007-05-04 21:54:46 +00001228 break;
1229 case BinaryOperator::EQ:
1230 case BinaryOperator::NE:
Chris Lattner256c21b2007-06-28 03:53:10 +00001231 ResultTy = CheckEqualityOperands(lhs, rhs, TokLoc);
Steve Naroff218bc2b2007-05-04 21:54:46 +00001232 break;
1233 case BinaryOperator::And:
1234 case BinaryOperator::Xor:
1235 case BinaryOperator::Or:
Chris Lattner256c21b2007-06-28 03:53:10 +00001236 ResultTy = CheckBitwiseOperands(lhs, rhs, TokLoc);
Steve Naroff218bc2b2007-05-04 21:54:46 +00001237 break;
1238 case BinaryOperator::LAnd:
1239 case BinaryOperator::LOr:
Chris Lattner256c21b2007-06-28 03:53:10 +00001240 ResultTy = CheckLogicalOperands(lhs, rhs, TokLoc);
Steve Naroff218bc2b2007-05-04 21:54:46 +00001241 break;
1242 case BinaryOperator::MulAssign:
1243 case BinaryOperator::DivAssign:
Chris Lattner256c21b2007-06-28 03:53:10 +00001244 CompTy = CheckMultiplyDivideOperands(lhs, rhs, TokLoc);
1245 if (!CompTy.isNull())
1246 ResultTy = CheckAssignmentOperands(lhs, rhs, TokLoc, CompTy);
Steve Naroff218bc2b2007-05-04 21:54:46 +00001247 break;
1248 case BinaryOperator::RemAssign:
Chris Lattner256c21b2007-06-28 03:53:10 +00001249 CompTy = CheckRemainderOperands(lhs, rhs, TokLoc);
1250 if (!CompTy.isNull())
1251 ResultTy = CheckAssignmentOperands(lhs, rhs, TokLoc, CompTy);
Steve Naroff218bc2b2007-05-04 21:54:46 +00001252 break;
1253 case BinaryOperator::AddAssign:
Chris Lattner256c21b2007-06-28 03:53:10 +00001254 CompTy = CheckAdditionOperands(lhs, rhs, TokLoc);
1255 if (!CompTy.isNull())
1256 ResultTy = CheckAssignmentOperands(lhs, rhs, TokLoc, CompTy);
Steve Naroff218bc2b2007-05-04 21:54:46 +00001257 break;
1258 case BinaryOperator::SubAssign:
Chris Lattner256c21b2007-06-28 03:53:10 +00001259 CompTy = CheckSubtractionOperands(lhs, rhs, TokLoc);
1260 if (!CompTy.isNull())
1261 ResultTy = CheckAssignmentOperands(lhs, rhs, TokLoc, CompTy);
Steve Naroff218bc2b2007-05-04 21:54:46 +00001262 break;
1263 case BinaryOperator::ShlAssign:
1264 case BinaryOperator::ShrAssign:
Chris Lattner256c21b2007-06-28 03:53:10 +00001265 CompTy = CheckShiftOperands(lhs, rhs, TokLoc);
1266 if (!CompTy.isNull())
1267 ResultTy = CheckAssignmentOperands(lhs, rhs, TokLoc, CompTy);
Steve Naroff218bc2b2007-05-04 21:54:46 +00001268 break;
1269 case BinaryOperator::AndAssign:
1270 case BinaryOperator::XorAssign:
1271 case BinaryOperator::OrAssign:
Chris Lattner256c21b2007-06-28 03:53:10 +00001272 CompTy = CheckBitwiseOperands(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::Comma:
Chris Lattner256c21b2007-06-28 03:53:10 +00001277 ResultTy = CheckCommaOperands(lhs, rhs, TokLoc);
Steve Naroff218bc2b2007-05-04 21:54:46 +00001278 break;
1279 }
Chris Lattner256c21b2007-06-28 03:53:10 +00001280 if (ResultTy.isNull())
Steve Naroff218bc2b2007-05-04 21:54:46 +00001281 return true;
Chris Lattner256c21b2007-06-28 03:53:10 +00001282 if (CompTy.isNull())
1283 return new BinaryOperator(lhs, rhs, Opc, ResultTy);
1284 else
Chris Lattner9369a562007-06-29 16:31:29 +00001285 return new CompoundAssignOperator(lhs, rhs, Opc, ResultTy, CompTy);
Steve Naroff218bc2b2007-05-04 21:54:46 +00001286}
1287
Steve Naroff35d85152007-05-07 00:24:15 +00001288// Unary Operators. 'Tok' is the token for the operator.
1289Action::ExprResult Sema::ParseUnaryOp(SourceLocation OpLoc, tok::TokenKind Op,
Chris Lattner86554282007-06-08 22:32:33 +00001290 ExprTy *input) {
1291 Expr *Input = (Expr*)input;
Steve Naroff35d85152007-05-07 00:24:15 +00001292 UnaryOperator::Opcode Opc = ConvertTokenKindToUnaryOpcode(Op);
1293 QualType resultType;
1294 switch (Opc) {
1295 default:
1296 assert(0 && "Unimplemented unary expr!");
1297 case UnaryOperator::PreInc:
1298 case UnaryOperator::PreDec:
Chris Lattner86554282007-06-08 22:32:33 +00001299 resultType = CheckIncrementDecrementOperand(Input, OpLoc);
Steve Naroff35d85152007-05-07 00:24:15 +00001300 break;
1301 case UnaryOperator::AddrOf:
Chris Lattner86554282007-06-08 22:32:33 +00001302 resultType = CheckAddressOfOperand(Input, OpLoc);
Steve Naroff35d85152007-05-07 00:24:15 +00001303 break;
1304 case UnaryOperator::Deref:
Chris Lattner86554282007-06-08 22:32:33 +00001305 resultType = CheckIndirectionOperand(Input, OpLoc);
Steve Naroff35d85152007-05-07 00:24:15 +00001306 break;
1307 case UnaryOperator::Plus:
1308 case UnaryOperator::Minus:
Chris Lattner86554282007-06-08 22:32:33 +00001309 resultType = UsualUnaryConversions(Input->getType());
Steve Naroff35d85152007-05-07 00:24:15 +00001310 if (!resultType->isArithmeticType()) // C99 6.5.3.3p1
Chris Lattnerc04bd6a2007-05-16 18:09:54 +00001311 return Diag(OpLoc, diag::err_typecheck_unary_expr,
1312 resultType.getAsString());
Steve Naroff35d85152007-05-07 00:24:15 +00001313 break;
1314 case UnaryOperator::Not: // bitwise complement
Steve Naroff1c8b7d32007-07-12 21:46:55 +00001315 resultType = UsualUnaryConversions(Input->getType());
1316 if (!resultType->isIntegerType()) // C99 6.5.3.3p1
1317 return Diag(OpLoc, diag::err_typecheck_unary_expr,
1318 resultType.getAsString());
Steve Naroff35d85152007-05-07 00:24:15 +00001319 break;
1320 case UnaryOperator::LNot: // logical negation
Steve Naroff71b59a92007-06-04 22:22:31 +00001321 // Unlike +/-/~, integer promotions aren't done here (C99 6.5.3.3p5).
Chris Lattner86554282007-06-08 22:32:33 +00001322 resultType = DefaultFunctionArrayConversion(Input->getType());
Steve Naroff35d85152007-05-07 00:24:15 +00001323 if (!resultType->isScalarType()) // C99 6.5.3.3p1
Chris Lattnerc04bd6a2007-05-16 18:09:54 +00001324 return Diag(OpLoc, diag::err_typecheck_unary_expr,
1325 resultType.getAsString());
Chris Lattnerbe31ed82007-06-02 19:11:33 +00001326 // LNot always has type int. C99 6.5.3.3p5.
1327 resultType = Context.IntTy;
Steve Naroff35d85152007-05-07 00:24:15 +00001328 break;
1329 case UnaryOperator::SizeOf:
Chris Lattner86554282007-06-08 22:32:33 +00001330 resultType = CheckSizeOfAlignOfOperand(Input->getType(), OpLoc, true);
Steve Naroff043d45d2007-05-15 02:32:35 +00001331 break;
Steve Naroff35d85152007-05-07 00:24:15 +00001332 case UnaryOperator::AlignOf:
Chris Lattner86554282007-06-08 22:32:33 +00001333 resultType = CheckSizeOfAlignOfOperand(Input->getType(), OpLoc, false);
1334 break;
1335 case UnaryOperator::Extension:
1336 // FIXME: does __extension__ cause any promotions? I would think not.
1337 resultType = Input->getType();
Steve Naroff043d45d2007-05-15 02:32:35 +00001338 break;
Steve Naroff35d85152007-05-07 00:24:15 +00001339 }
1340 if (resultType.isNull())
1341 return true;
Chris Lattner86554282007-06-08 22:32:33 +00001342 return new UnaryOperator(Input, Opc, resultType, OpLoc);
Steve Naroff35d85152007-05-07 00:24:15 +00001343}
Chris Lattnereefa10e2007-05-28 06:56:27 +00001344
1345/// ParseAddrLabel - Parse the GNU address of label extension: "&&foo".
1346Sema::ExprResult Sema::ParseAddrLabel(SourceLocation OpLoc,
1347 SourceLocation LabLoc,
1348 IdentifierInfo *LabelII) {
1349 // Look up the record for this label identifier.
1350 LabelStmt *&LabelDecl = LabelMap[LabelII];
1351
1352 // If we haven't seen this label yet, create a forward reference.
1353 if (LabelDecl == 0)
1354 LabelDecl = new LabelStmt(LabLoc, LabelII, 0);
1355
1356 // Create the AST node. The address of a label always has type 'void*'.
1357 return new AddrLabel(OpLoc, LabLoc, LabelDecl,
1358 Context.getPointerType(Context.VoidTy));
1359}
1360