blob: fc568b6fd400e74381c79c1cab810ba0b89f5284 [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 Narofff1e53692007-03-23 22:27:02 +0000211 // FIXME: fill in the value and compute the real type...
Steve Naroff509fe022007-05-17 01:16:00 +0000212 return new FloatingLiteral(7.7, Context.FloatTy, Tok.getLocation());
Steve Naroff09ef4742007-03-09 23:16:33 +0000213 }
Steve Narofff2fb89e2007-03-13 20:29:44 +0000214 return ExprResult(true);
Chris Lattnere168f762006-11-10 05:29:30 +0000215}
216
217Action::ExprResult Sema::ParseParenExpr(SourceLocation L, SourceLocation R,
218 ExprTy *Val) {
Steve Naroffae4143e2007-04-26 20:39:23 +0000219 Expr *e = (Expr *)Val;
220 assert((e != 0) && "ParseParenExpr() missing expr");
Steve Naroff53f07dc2007-05-17 21:49:33 +0000221 return new ParenExpr(L, R, e);
Chris Lattnere168f762006-11-10 05:29:30 +0000222}
223
Steve Naroff71b59a92007-06-04 22:22:31 +0000224/// The UsualUnaryConversions() function is *not* called by this routine.
Steve Naroffa78fe7e2007-05-16 19:47:19 +0000225/// See C99 6.3.2.1p[2-4] for more details.
Steve Naroff043d45d2007-05-15 02:32:35 +0000226QualType Sema::CheckSizeOfAlignOfOperand(QualType exprType,
227 SourceLocation OpLoc, bool isSizeof) {
228 // C99 6.5.3.4p1:
229 if (isa<FunctionType>(exprType) && isSizeof)
230 // alignof(function) is allowed.
231 Diag(OpLoc, diag::ext_sizeof_function_type);
232 else if (exprType->isVoidType())
233 Diag(OpLoc, diag::ext_sizeof_void_type, isSizeof ? "sizeof" : "__alignof");
234 else if (exprType->isIncompleteType()) {
Steve Naroff043d45d2007-05-15 02:32:35 +0000235 Diag(OpLoc, isSizeof ? diag::err_sizeof_incomplete_type :
Chris Lattner3dc3d772007-05-16 18:07:12 +0000236 diag::err_alignof_incomplete_type,
237 exprType.getAsString());
Steve Naroff043d45d2007-05-15 02:32:35 +0000238 return QualType(); // error
239 }
240 // C99 6.5.3.4p4: the type (an unsigned integer type) is size_t.
241 return Context.getSizeType();
242}
243
Chris Lattnere168f762006-11-10 05:29:30 +0000244Action::ExprResult Sema::
245ParseSizeOfAlignOfTypeExpr(SourceLocation OpLoc, bool isSizeof,
Steve Naroff509fe022007-05-17 01:16:00 +0000246 SourceLocation LPLoc, TypeTy *Ty,
247 SourceLocation RPLoc) {
Chris Lattner0d8b1a12006-11-20 04:34:45 +0000248 // If error parsing type, ignore.
249 if (Ty == 0) return true;
Chris Lattner6531c102007-01-23 22:29:49 +0000250
251 // Verify that this is a valid expression.
Steve Naroffe5aa9be2007-04-05 22:36:20 +0000252 QualType ArgTy = QualType::getFromOpaquePtr(Ty);
Chris Lattner6531c102007-01-23 22:29:49 +0000253
Steve Naroff043d45d2007-05-15 02:32:35 +0000254 QualType resultType = CheckSizeOfAlignOfOperand(ArgTy, OpLoc, isSizeof);
255
256 if (resultType.isNull())
257 return true;
Steve Naroff509fe022007-05-17 01:16:00 +0000258 return new SizeOfAlignOfTypeExpr(isSizeof, ArgTy, resultType, OpLoc, RPLoc);
Chris Lattnere168f762006-11-10 05:29:30 +0000259}
260
261
262Action::ExprResult Sema::ParsePostfixUnaryOp(SourceLocation OpLoc,
263 tok::TokenKind Kind,
264 ExprTy *Input) {
265 UnaryOperator::Opcode Opc;
266 switch (Kind) {
267 default: assert(0 && "Unknown unary op!");
268 case tok::plusplus: Opc = UnaryOperator::PostInc; break;
269 case tok::minusminus: Opc = UnaryOperator::PostDec; break;
270 }
Steve Naroff71ce2e02007-05-18 22:53:50 +0000271 QualType result = CheckIncrementDecrementOperand((Expr *)Input, OpLoc);
Steve Naroff35d85152007-05-07 00:24:15 +0000272 if (result.isNull())
273 return true;
Steve Naroff509fe022007-05-17 01:16:00 +0000274 return new UnaryOperator((Expr *)Input, Opc, result, OpLoc);
Chris Lattnere168f762006-11-10 05:29:30 +0000275}
276
277Action::ExprResult Sema::
278ParseArraySubscriptExpr(ExprTy *Base, SourceLocation LLoc,
279 ExprTy *Idx, SourceLocation RLoc) {
Steve Naroffe5aa9be2007-04-05 22:36:20 +0000280 QualType t1 = ((Expr *)Base)->getType();
281 QualType t2 = ((Expr *)Idx)->getType();
Steve Narofff1e53692007-03-23 22:27:02 +0000282
283 assert(!t1.isNull() && "no type for array base expression");
Steve Naroffc1aadb12007-03-28 21:49:40 +0000284 assert(!t2.isNull() && "no type for array index expression");
Steve Narofff1e53692007-03-23 22:27:02 +0000285
Steve Naroffb3096442007-06-09 03:47:53 +0000286 QualType canonT1 = DefaultFunctionArrayConversion(t1).getCanonicalType();
287 QualType canonT2 = DefaultFunctionArrayConversion(t2).getCanonicalType();
Steve Naroffd50c88e2007-04-05 21:15:20 +0000288
Steve Naroffc1aadb12007-03-28 21:49:40 +0000289 // C99 6.5.2.1p2: the expression e1[e2] is by definition precisely equivalent
290 // to the expression *((e1)+(e2)). This means the array "Base" may actually be
Steve Narofff1e53692007-03-23 22:27:02 +0000291 // in the subscript position. As a result, we need to derive the array base
292 // and index from the expression types.
293
Steve Naroffb3096442007-06-09 03:47:53 +0000294 Expr *baseExpr, *indexExpr;
Steve Naroffe5aa9be2007-04-05 22:36:20 +0000295 QualType baseType, indexType;
Steve Naroffb3096442007-06-09 03:47:53 +0000296 if (isa<PointerType>(canonT1)) {
Steve Naroffd50c88e2007-04-05 21:15:20 +0000297 baseType = canonT1;
298 indexType = canonT2;
Steve Naroffb3096442007-06-09 03:47:53 +0000299 baseExpr = static_cast<Expr *>(Base);
300 indexExpr = static_cast<Expr *>(Idx);
301 } else if (isa<PointerType>(canonT2)) { // uncommon
Steve Naroffd50c88e2007-04-05 21:15:20 +0000302 baseType = canonT2;
303 indexType = canonT1;
Steve Naroffb3096442007-06-09 03:47:53 +0000304 baseExpr = static_cast<Expr *>(Idx);
305 indexExpr = static_cast<Expr *>(Base);
306 } else {
307 return Diag(baseExpr->getLocStart(), diag::err_typecheck_subscript_value,
308 baseExpr->getSourceRange());
309 }
Steve Naroffc1aadb12007-03-28 21:49:40 +0000310 // C99 6.5.2.1p1
Steve Naroffb3096442007-06-09 03:47:53 +0000311 if (!indexType->isIntegerType()) {
312 return Diag(indexExpr->getLocStart(), diag::err_typecheck_subscript,
313 indexExpr->getSourceRange());
314 }
Steve Naroff95af0132007-03-30 23:47:58 +0000315 // FIXME: need to deal with const...
Steve Naroffb3096442007-06-09 03:47:53 +0000316 PointerType *ary = cast<PointerType>(baseType);
317 QualType resultType = ary->getPointeeType();
318 // in practice, the following check catches trying to index a pointer
319 // to a function (e.g. void (*)(int)). Functions are not objects in c99.
320 if (!resultType->isObjectType()) {
321 return Diag(baseExpr->getLocStart(),
322 diag::err_typecheck_subscript_not_object,
323 baseType.getAsString(), baseExpr->getSourceRange());
324 }
Steve Naroff509fe022007-05-17 01:16:00 +0000325 return new ArraySubscriptExpr((Expr*)Base, (Expr*)Idx, resultType, RLoc);
Chris Lattnere168f762006-11-10 05:29:30 +0000326}
327
328Action::ExprResult Sema::
329ParseMemberReferenceExpr(ExprTy *Base, SourceLocation OpLoc,
330 tok::TokenKind OpKind, SourceLocation MemberLoc,
331 IdentifierInfo &Member) {
Steve Naroffe5aa9be2007-04-05 22:36:20 +0000332 QualType qualifiedType = ((Expr *)Base)->getType();
Steve Naroffca8f7122007-04-01 01:41:35 +0000333
334 assert(!qualifiedType.isNull() && "no type for member expression");
335
Steve Naroffe5aa9be2007-04-05 22:36:20 +0000336 QualType canonType = qualifiedType.getCanonicalType();
Steve Narofff1e53692007-03-23 22:27:02 +0000337
338 if (OpKind == tok::arrow) {
Steve Naroffca8f7122007-04-01 01:41:35 +0000339 if (PointerType *PT = dyn_cast<PointerType>(canonType)) {
340 qualifiedType = PT->getPointeeType();
Steve Naroffd50c88e2007-04-05 21:15:20 +0000341 canonType = qualifiedType.getCanonicalType();
Steve Naroffca8f7122007-04-01 01:41:35 +0000342 } else
Steve Narofff1e53692007-03-23 22:27:02 +0000343 return Diag(OpLoc, diag::err_typecheck_member_reference_arrow);
344 }
Steve Naroff92e30f82007-04-02 22:35:25 +0000345 if (!isa<RecordType>(canonType))
346 return Diag(OpLoc, diag::err_typecheck_member_reference_structUnion);
347
348 // get the struct/union definition from the type.
349 RecordDecl *RD = cast<RecordType>(canonType)->getDecl();
Steve Naroffcc321422007-03-26 23:09:51 +0000350
Steve Naroff92e30f82007-04-02 22:35:25 +0000351 if (canonType->isIncompleteType())
352 return Diag(OpLoc, diag::err_typecheck_incomplete_tag, RD->getName());
Steve Naroffcc321422007-03-26 23:09:51 +0000353
Steve Naroff92e30f82007-04-02 22:35:25 +0000354 FieldDecl *MemberDecl = RD->getMember(&Member);
355 if (!MemberDecl)
356 return Diag(OpLoc, diag::err_typecheck_no_member, Member.getName());
357
Steve Naroff9358c712007-05-27 23:58:33 +0000358 return new MemberExpr((Expr*)Base, OpKind == tok::arrow,
359 MemberDecl, MemberLoc);
Chris Lattnere168f762006-11-10 05:29:30 +0000360}
361
362/// ParseCallExpr - Handle a call to Fn with the specified array of arguments.
363/// This provides the location of the left/right parens and a list of comma
364/// locations.
365Action::ExprResult Sema::
366ParseCallExpr(ExprTy *Fn, SourceLocation LParenLoc,
Steve Naroffb8c289d2007-05-08 22:18:00 +0000367 ExprTy **Args, unsigned NumArgsInCall,
Chris Lattnere168f762006-11-10 05:29:30 +0000368 SourceLocation *CommaLocs, SourceLocation RParenLoc) {
Steve Naroff8563f652007-05-28 19:25:56 +0000369 Expr *funcExpr = (Expr *)Fn;
Steve Naroff8563f652007-05-28 19:25:56 +0000370 assert(funcExpr && "no function call expression");
371
Chris Lattner3343f812007-06-06 05:05:41 +0000372 QualType qType = UsualUnaryConversions(funcExpr->getType());
Steve Naroffae4143e2007-04-26 20:39:23 +0000373 assert(!qType.isNull() && "no type for function call expression");
374
Chris Lattner3d01e4e2007-06-06 05:14:05 +0000375 // C99 6.5.2.2p1 - "The expression that denotes the called function shall have
376 // type pointer to function".
377 const PointerType *PT = dyn_cast<PointerType>(qType);
378 if (PT == 0) PT = dyn_cast<PointerType>(qType.getCanonicalType());
379
380 if (PT == 0)
381 return Diag(funcExpr->getLocStart(), diag::err_typecheck_call_not_function,
382 SourceRange(funcExpr->getLocStart(), RParenLoc));
Chris Lattner3343f812007-06-06 05:05:41 +0000383
384 const FunctionType *funcT = dyn_cast<FunctionType>(PT->getPointeeType());
Chris Lattner3d01e4e2007-06-06 05:14:05 +0000385 if (funcT == 0)
386 funcT = dyn_cast<FunctionType>(PT->getPointeeType().getCanonicalType());
387
388 if (funcT == 0)
389 return Diag(funcExpr->getLocStart(), diag::err_typecheck_call_not_function,
390 SourceRange(funcExpr->getLocStart(), RParenLoc));
Steve Naroffb8c289d2007-05-08 22:18:00 +0000391
Steve Naroff17f76e02007-05-03 21:03:48 +0000392 // If a prototype isn't declared, the parser implicitly defines a func decl
393 QualType resultType = funcT->getResultType();
394
395 if (const FunctionTypeProto *proto = dyn_cast<FunctionTypeProto>(funcT)) {
396 // C99 6.5.2.2p7 - the arguments are implicitly converted, as if by
397 // assignment, to the types of the corresponding parameter, ...
398
399 unsigned NumArgsInProto = proto->getNumArgs();
Steve Naroffb8c289d2007-05-08 22:18:00 +0000400 unsigned NumArgsToCheck = NumArgsInCall;
Steve Naroff17f76e02007-05-03 21:03:48 +0000401
Steve Naroffb8c289d2007-05-08 22:18:00 +0000402 if (NumArgsInCall < NumArgsInProto)
Steve Naroff56faab22007-05-30 04:20:12 +0000403 Diag(RParenLoc, diag::err_typecheck_call_too_few_args,
Steve Naroffeb9da942007-05-30 00:06:37 +0000404 funcExpr->getSourceRange());
Steve Naroffb8c289d2007-05-08 22:18:00 +0000405 else if (NumArgsInCall > NumArgsInProto) {
Steve Naroff8563f652007-05-28 19:25:56 +0000406 if (!proto->isVariadic()) {
Steve Naroff56faab22007-05-30 04:20:12 +0000407 Diag(((Expr **)Args)[NumArgsInProto+1]->getLocStart(),
408 diag::err_typecheck_call_too_many_args, funcExpr->getSourceRange(),
409 ((Expr **)Args)[NumArgsInProto+1]->getSourceRange());
Steve Naroff8563f652007-05-28 19:25:56 +0000410 }
Steve Naroffb8c289d2007-05-08 22:18:00 +0000411 NumArgsToCheck = NumArgsInProto;
Steve Naroff17f76e02007-05-03 21:03:48 +0000412 }
413 // Continue to check argument types (even if we have too few/many args).
Steve Naroffb8c289d2007-05-08 22:18:00 +0000414 for (unsigned i = 0; i < NumArgsToCheck; i++) {
Steve Naroff8563f652007-05-28 19:25:56 +0000415 Expr *argExpr = ((Expr **)Args)[i];
Steve Naroff8563f652007-05-28 19:25:56 +0000416 assert(argExpr && "ParseCallExpr(): missing argument expression");
417
Steve Naroff17f76e02007-05-03 21:03:48 +0000418 QualType lhsType = proto->getArgType(i);
Steve Naroff8563f652007-05-28 19:25:56 +0000419 QualType rhsType = argExpr->getType();
Steve Naroff17f76e02007-05-03 21:03:48 +0000420
421 if (lhsType == rhsType) // common case, fast path...
422 continue;
Steve Naroff98cf3e92007-06-06 18:38:38 +0000423
424 AssignmentCheckResult result = CheckAssignmentConstraints(lhsType,
425 rhsType);
Steve Naroff6f49f5d2007-05-29 14:23:36 +0000426 SourceLocation l = argExpr->getLocStart();
Steve Naroff17f76e02007-05-03 21:03:48 +0000427
428 // decode the result (notice that AST's are still created for extensions).
Steve Naroff17f76e02007-05-03 21:03:48 +0000429 switch (result) {
430 case Compatible:
431 break;
432 case PointerFromInt:
Steve Naroff218bc2b2007-05-04 21:54:46 +0000433 // check for null pointer constant (C99 6.3.2.3p3)
Steve Naroffeb9da942007-05-30 00:06:37 +0000434 if (!argExpr->isNullPointerConstant()) {
Steve Naroff56faab22007-05-30 04:20:12 +0000435 Diag(l, diag::ext_typecheck_passing_pointer_int,
436 lhsType.getAsString(), rhsType.getAsString(),
Steve Naroffeb9da942007-05-30 00:06:37 +0000437 funcExpr->getSourceRange(), argExpr->getSourceRange());
438 }
Steve Naroff17f76e02007-05-03 21:03:48 +0000439 break;
440 case IntFromPointer:
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());
Steve Naroff17f76e02007-05-03 21:03:48 +0000444 break;
445 case IncompatiblePointer:
Steve Naroff8563f652007-05-28 19:25:56 +0000446 Diag(l, diag::ext_typecheck_passing_incompatible_pointer,
Steve Naroffeb9da942007-05-30 00:06:37 +0000447 rhsType.getAsString(), lhsType.getAsString(),
448 funcExpr->getSourceRange(), argExpr->getSourceRange());
Steve Naroff17f76e02007-05-03 21:03:48 +0000449 break;
Steve Naroff1f4d7272007-05-11 04:00:31 +0000450 case CompatiblePointerDiscardsQualifiers:
Steve Naroffeb9da942007-05-30 00:06:37 +0000451 Diag(l, diag::ext_typecheck_passing_discards_qualifiers,
452 rhsType.getAsString(), lhsType.getAsString(),
453 funcExpr->getSourceRange(), argExpr->getSourceRange());
Steve Naroff1f4d7272007-05-11 04:00:31 +0000454 break;
Steve Naroff17f76e02007-05-03 21:03:48 +0000455 case Incompatible:
Steve Naroff8563f652007-05-28 19:25:56 +0000456 return Diag(l, diag::err_typecheck_passing_incompatible,
457 rhsType.getAsString(), lhsType.getAsString(),
458 funcExpr->getSourceRange(), argExpr->getSourceRange());
Steve Naroff17f76e02007-05-03 21:03:48 +0000459 }
460 }
Steve Naroffb8c289d2007-05-08 22:18:00 +0000461 // Even if the types checked, bail if we had the wrong number of arguments.
462 if ((NumArgsInCall != NumArgsInProto) && !proto->isVariadic())
463 return true;
Steve Naroffae4143e2007-04-26 20:39:23 +0000464 }
Steve Naroff509fe022007-05-17 01:16:00 +0000465 return new CallExpr((Expr*)Fn, (Expr**)Args, NumArgsInCall, resultType,
466 RParenLoc);
Chris Lattnere168f762006-11-10 05:29:30 +0000467}
468
469Action::ExprResult Sema::
470ParseCastExpr(SourceLocation LParenLoc, TypeTy *Ty,
471 SourceLocation RParenLoc, ExprTy *Op) {
Chris Lattner0d8b1a12006-11-20 04:34:45 +0000472 // If error parsing type, ignore.
Steve Naroffae4143e2007-04-26 20:39:23 +0000473 assert((Ty != 0) && "ParseCastExpr(): missing type");
Chris Lattner1d411a82007-06-05 20:52:21 +0000474 // FIXME: Sema for cast is completely missing.
Steve Naroff509fe022007-05-17 01:16:00 +0000475 return new CastExpr(QualType::getFromOpaquePtr(Ty), (Expr*)Op, LParenLoc);
Chris Lattnere168f762006-11-10 05:29:30 +0000476}
477
Steve Narofff8a28c52007-05-15 20:29:32 +0000478inline QualType Sema::CheckConditionalOperands( // C99 6.5.15
479 Expr *Cond, Expr *LHS, Expr *RHS, SourceLocation questionLoc) {
Steve Naroffa78fe7e2007-05-16 19:47:19 +0000480 QualType cond = Cond->getType();
481 QualType lhs = LHS->getType();
482 QualType rhs = RHS->getType();
483
Steve Narofff8a28c52007-05-15 20:29:32 +0000484 assert(!cond.isNull() && "ParseConditionalOp(): no conditional type");
485 assert(!lhs.isNull() && "ParseConditionalOp(): no lhs type");
486 assert(!rhs.isNull() && "ParseConditionalOp(): no rhs type");
487
Steve Naroff71b59a92007-06-04 22:22:31 +0000488 cond = UsualUnaryConversions(cond);
489 lhs = UsualUnaryConversions(lhs);
490 rhs = UsualUnaryConversions(rhs);
Steve Naroffa78fe7e2007-05-16 19:47:19 +0000491
492 // first, check the condition.
493 if (!cond->isScalarType()) { // C99 6.5.15p2
Steve Naroff53f07dc2007-05-17 21:49:33 +0000494 Diag(Cond->getLocStart(), diag::err_typecheck_cond_expect_scalar,
Steve Naroff509fe022007-05-17 01:16:00 +0000495 cond.getAsString());
Steve Naroffa78fe7e2007-05-16 19:47:19 +0000496 return QualType();
497 }
498 // now check the two expressions.
499 if (lhs->isArithmeticType() && rhs->isArithmeticType()) // C99 6.5.15p3,5
500 return UsualArithmeticConversions(lhs, rhs);
501
502 if ((lhs->isStructureType() && rhs->isStructureType()) || // C99 6.5.15p3
503 (lhs->isUnionType() && rhs->isUnionType())) {
504 TagType *lTag = cast<TagType>(lhs.getCanonicalType());
505 TagType *rTag = cast<TagType>(rhs.getCanonicalType());
506
507 if (lTag->getDecl()->getIdentifier() == rTag->getDecl()->getIdentifier())
508 return lhs;
509 else {
510 Diag(questionLoc, diag::err_typecheck_cond_incompatible_operands,
Steve Naroff3c87a572007-05-28 19:40:22 +0000511 lhs.getAsString(), rhs.getAsString(),
512 LHS->getSourceRange(), RHS->getSourceRange());
Steve Naroffa78fe7e2007-05-16 19:47:19 +0000513 return QualType();
514 }
515 }
Steve Naroff30d1fbc2007-05-20 19:46:53 +0000516 if (lhs->isPointerType() && RHS->isNullPointerConstant()) // C99 6.5.15p3
517 return lhs;
518 if (rhs->isPointerType() && LHS->isNullPointerConstant())
519 return rhs;
520
Steve Naroffa78fe7e2007-05-16 19:47:19 +0000521 if (lhs->isPointerType() && rhs->isPointerType()) { // C99 6.5.15p3,6
522 QualType lhptee, rhptee;
523
524 // get the "pointed to" type
525 lhptee = cast<PointerType>(lhs.getCanonicalType())->getPointeeType();
526 rhptee = cast<PointerType>(rhs.getCanonicalType())->getPointeeType();
527
528 // ignore qualifiers on void (C99 6.5.15p3, clause 6)
529 if (lhptee.getUnqualifiedType()->isVoidType() &&
530 (rhptee->isObjectType() || rhptee->isIncompleteType()))
531 return lhs;
532 if (rhptee.getUnqualifiedType()->isVoidType() &&
533 (lhptee->isObjectType() || lhptee->isIncompleteType()))
534 return rhs;
535
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 Naroffeb9da942007-05-30 00:06:37 +0000543 lhs.getAsString(), rhs.getAsString(),
544 LHS->getSourceRange(), RHS->getSourceRange());
Steve Naroffa78fe7e2007-05-16 19:47:19 +0000545 return lhs; // FIXME: this is an _ext - is this return o.k?
546 }
547 }
548 if (lhs->isVoidType() && rhs->isVoidType()) // C99 6.5.15p3
549 return lhs;
Steve Naroffa78fe7e2007-05-16 19:47:19 +0000550
551 Diag(questionLoc, diag::err_typecheck_cond_incompatible_operands,
Steve Naroffeb9da942007-05-30 00:06:37 +0000552 lhs.getAsString(), rhs.getAsString(),
553 LHS->getSourceRange(), RHS->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 Narofff8a28c52007-05-15 20:29:32 +0000563 QualType result = CheckConditionalOperands((Expr *)Cond, (Expr *)LHS,
564 (Expr *)RHS, QuestionLoc);
565 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 Naroff71b59a92007-06-04 22:22:31 +0000570inline QualType Sema::DefaultFunctionArrayConversion(QualType t) {
Steve Naroffa78fe7e2007-05-16 19:47:19 +0000571 if (t->isFunctionType()) // C99 6.3.2.1p4
Chris Lattnerea3c20b2007-06-02 22:47:04 +0000572 return Context.getPointerType(t);
Steve Naroffa78fe7e2007-05-16 19:47:19 +0000573 if (const ArrayType *ary = dyn_cast<ArrayType>(t.getCanonicalType()))
574 return Context.getPointerType(ary->getElementType()); // C99 6.3.2.1p3
Steve Naroff1926c832007-04-24 00:23:05 +0000575 return t;
576}
577
Steve Naroff71b59a92007-06-04 22:22:31 +0000578/// UsualUnaryConversion - Performs various conversions that are common to most
579/// operators (C99 6.3). The conversions of array and function types are
580/// sometimes surpressed. For example, the array->pointer conversion doesn't
581/// apply if the array is an argument to the sizeof or address (&) operators.
582/// In these instances, this routine should *not* be called.
583QualType Sema::UsualUnaryConversions(QualType t) {
584 assert(!t.isNull() && "UsualUnaryConversions - missing type");
585
586 if (t->isPromotableIntegerType()) // C99 6.3.1.1p2
587 return Context.IntTy;
588 return DefaultFunctionArrayConversion(t);
589}
590
Steve Naroff1926c832007-04-24 00:23:05 +0000591/// UsualArithmeticConversions - Performs various conversions that are common to
592/// binary operators (C99 6.3.1.8). If both operands aren't arithmetic, this
593/// routine returns the first non-arithmetic type found. The client is
594/// responsible for emitting appropriate error diagnostics.
Steve Naroffd6fbee82007-06-13 15:42:33 +0000595QualType Sema::UsualArithmeticConversions(QualType &lhs, QualType &rhs) {
596 lhs = UsualUnaryConversions(lhs);
597 rhs = UsualUnaryConversions(rhs);
Steve Naroff1926c832007-04-24 00:23:05 +0000598
Chris Lattner0c46c5d2007-06-02 23:53:17 +0000599 // If both types are identical, no conversion is needed.
600 if (lhs == rhs)
601 return lhs;
602
603 // If either side is a non-arithmetic type (e.g. a pointer), we are done.
604 // The caller can deal with this (e.g. pointer + int).
Steve Naroffb01bbe32007-04-25 01:22:31 +0000605 if (!lhs->isArithmeticType())
606 return lhs;
Steve Narofff633d092007-04-25 19:01:39 +0000607 if (!rhs->isArithmeticType())
Steve Naroffb01bbe32007-04-25 01:22:31 +0000608 return rhs;
Steve Naroff1926c832007-04-24 00:23:05 +0000609
Chris Lattner0c46c5d2007-06-02 23:53:17 +0000610 // At this point, we have two different arithmetic types.
Steve Naroffb01bbe32007-04-25 01:22:31 +0000611
612 // Handle complex types first (C99 6.3.1.8p1).
613 if (lhs->isComplexType() || rhs->isComplexType()) {
614 // if we have an integer operand, the result is the complex type.
615 if (rhs->isIntegerType())
616 return lhs;
617 if (lhs->isIntegerType())
618 return rhs;
619
Steve Naroffe4718892007-04-27 18:30:00 +0000620 return Context.maxComplexType(lhs, rhs);
Steve Naroffbf223ba2007-04-24 20:56:26 +0000621 }
Chris Lattner0c46c5d2007-06-02 23:53:17 +0000622
Steve Naroffb01bbe32007-04-25 01:22:31 +0000623 // Now handle "real" floating types (i.e. float, double, long double).
624 if (lhs->isRealFloatingType() || rhs->isRealFloatingType()) {
625 // if we have an integer operand, the result is the real floating type.
626 if (rhs->isIntegerType())
627 return lhs;
628 if (lhs->isIntegerType())
629 return rhs;
630
631 // we have two real floating types, float/complex combos were handled above.
Steve Naroffe4718892007-04-27 18:30:00 +0000632 return Context.maxFloatingType(lhs, rhs);
Steve Naroffb01bbe32007-04-25 01:22:31 +0000633 }
Steve Naroffe4718892007-04-27 18:30:00 +0000634 return Context.maxIntegerType(lhs, rhs);
Steve Narofff1e53692007-03-23 22:27:02 +0000635}
636
Steve Naroff3f597292007-05-11 22:18:03 +0000637// CheckPointerTypesForAssignment - This is a very tricky routine (despite
638// being closely modeled after the C99 spec:-). The odd characteristic of this
639// routine is it effectively iqnores the qualifiers on the top level pointee.
640// This circumvents the usual type rules specified in 6.2.7p1 & 6.7.5.[1-3].
641// FIXME: add a couple examples in this comment.
Steve Naroff98cf3e92007-06-06 18:38:38 +0000642Sema::AssignmentCheckResult
643Sema::CheckPointerTypesForAssignment(QualType lhsType, QualType rhsType) {
Steve Naroff3f597292007-05-11 22:18:03 +0000644 QualType lhptee, rhptee;
Steve Naroff1f4d7272007-05-11 04:00:31 +0000645
646 // get the "pointed to" type (ignoring qualifiers at the top level)
Steve Naroff3f597292007-05-11 22:18:03 +0000647 lhptee = cast<PointerType>(lhsType.getCanonicalType())->getPointeeType();
648 rhptee = cast<PointerType>(rhsType.getCanonicalType())->getPointeeType();
Steve Naroff1f4d7272007-05-11 04:00:31 +0000649
650 // make sure we operate on the canonical type
Steve Naroff3f597292007-05-11 22:18:03 +0000651 lhptee = lhptee.getCanonicalType();
652 rhptee = rhptee.getCanonicalType();
Steve Naroff1f4d7272007-05-11 04:00:31 +0000653
Steve Naroff98cf3e92007-06-06 18:38:38 +0000654 AssignmentCheckResult r = Compatible;
655
Steve Naroff3f597292007-05-11 22:18:03 +0000656 // C99 6.5.16.1p1: This following citation is common to constraints
657 // 3 & 4 (below). ...and the type *pointed to* by the left has all the
658 // qualifiers of the type *pointed to* by the right;
659 if ((lhptee.getQualifiers() & rhptee.getQualifiers()) !=
660 rhptee.getQualifiers())
661 r = CompatiblePointerDiscardsQualifiers;
662
663 // C99 6.5.16.1p1 (constraint 4): If one operand is a pointer to an object or
664 // incomplete type and the other is a pointer to a qualified or unqualified
665 // version of void...
666 if (lhptee.getUnqualifiedType()->isVoidType() &&
667 (rhptee->isObjectType() || rhptee->isIncompleteType()))
668 ;
669 else if (rhptee.getUnqualifiedType()->isVoidType() &&
670 (lhptee->isObjectType() || lhptee->isIncompleteType()))
671 ;
672 // C99 6.5.16.1p1 (constraint 3): both operands are pointers to qualified or
673 // unqualified versions of compatible types, ...
674 else if (!Type::typesAreCompatible(lhptee.getUnqualifiedType(),
675 rhptee.getUnqualifiedType()))
676 r = IncompatiblePointer; // this "trumps" PointerAssignDiscardsQualifiers
Steve Naroff98cf3e92007-06-06 18:38:38 +0000677 return r;
Steve Naroff1f4d7272007-05-11 04:00:31 +0000678}
679
Steve Naroff98cf3e92007-06-06 18:38:38 +0000680/// CheckAssignmentConstraints (C99 6.5.16) - This routine currently
Steve Naroff17f76e02007-05-03 21:03:48 +0000681/// has code to accommodate several GCC extensions when type checking
682/// pointers. Here are some objectionable examples that GCC considers warnings:
683///
684/// int a, *pint;
685/// short *pshort;
686/// struct foo *pfoo;
687///
688/// pint = pshort; // warning: assignment from incompatible pointer type
689/// a = pint; // warning: assignment makes integer from pointer without a cast
690/// pint = a; // warning: assignment makes pointer from integer without a cast
691/// pint = pfoo; // warning: assignment from incompatible pointer type
692///
693/// As a result, the code for dealing with pointers is more complex than the
694/// C99 spec dictates.
695/// Note: the warning above turn into errors when -pedantic-errors is enabled.
696///
Steve Naroff98cf3e92007-06-06 18:38:38 +0000697Sema::AssignmentCheckResult
698Sema::CheckAssignmentConstraints(QualType lhsType, QualType rhsType) {
Bill Wendling216423b2007-05-30 06:30:29 +0000699 // This check seems unnatural, however it is necessary to insure the proper
700 // conversion of functions/arrays. If the conversion were done for all
701 // DeclExpr's (created by ParseIdentifierExpr), it would mess up the unary
702 // expressions that surpress this implicit conversion (&, sizeof).
Steve Naroff71b59a92007-06-04 22:22:31 +0000703 rhsType = DefaultFunctionArrayConversion(rhsType);
Steve Naroffb891de32007-05-02 23:51:10 +0000704
Steve Naroff9eb24652007-05-02 21:58:15 +0000705 if (lhsType->isArithmeticType() && rhsType->isArithmeticType())
Steve Naroff98cf3e92007-06-06 18:38:38 +0000706 return Compatible;
Steve Naroff9eb24652007-05-02 21:58:15 +0000707 else if (lhsType->isPointerType()) {
Steve Naroff98cf3e92007-06-06 18:38:38 +0000708 if (rhsType->isIntegerType())
709 return PointerFromInt;
710
Steve Naroff3f597292007-05-11 22:18:03 +0000711 if (rhsType->isPointerType())
Steve Naroff98cf3e92007-06-06 18:38:38 +0000712 return CheckPointerTypesForAssignment(lhsType, rhsType);
Steve Naroff9eb24652007-05-02 21:58:15 +0000713 } else if (rhsType->isPointerType()) {
Steve Naroff98cf3e92007-06-06 18:38:38 +0000714 // C99 6.5.16.1p1: the left operand is _Bool and the right is a pointer.
715 if ((lhsType->isIntegerType()) && (lhsType != Context.BoolTy))
716 return IntFromPointer;
717
Steve Naroff3f597292007-05-11 22:18:03 +0000718 if (lhsType->isPointerType())
Steve Naroff98cf3e92007-06-06 18:38:38 +0000719 return CheckPointerTypesForAssignment(lhsType, rhsType);
Steve Naroff1f4d7272007-05-11 04:00:31 +0000720 } else if (isa<TagType>(lhsType) && isa<TagType>(rhsType)) {
721 if (Type::tagTypesAreCompatible(lhsType, rhsType))
Steve Naroff98cf3e92007-06-06 18:38:38 +0000722 return Compatible;
Bill Wendlingdb4f06e2007-06-02 23:29:59 +0000723 } else if (lhsType->isReferenceType() || rhsType->isReferenceType()) {
724 if (Type::referenceTypesAreCompatible(lhsType, rhsType))
Steve Naroff98cf3e92007-06-06 18:38:38 +0000725 return Compatible;
Bill Wendling216423b2007-05-30 06:30:29 +0000726 }
Steve Naroff98cf3e92007-06-06 18:38:38 +0000727 return Incompatible;
Steve Naroff9eb24652007-05-02 21:58:15 +0000728}
729
Steve Naroff6f49f5d2007-05-29 14:23:36 +0000730inline void Sema::InvalidOperands(SourceLocation loc, Expr *lex, Expr *rex) {
731 Diag(loc, diag::err_typecheck_invalid_operands,
732 lex->getType().getAsString(), rex->getType().getAsString(),
733 lex->getSourceRange(), rex->getSourceRange());
734}
735
Steve Naroff218bc2b2007-05-04 21:54:46 +0000736inline QualType Sema::CheckMultiplyDivideOperands(
737 Expr *lex, Expr *rex, SourceLocation loc)
Steve Naroff5c10d4b2007-04-20 23:42:24 +0000738{
Steve Naroffd6fbee82007-06-13 15:42:33 +0000739 QualType lhsType = lex->getType(), rhsType = rex->getType();
740 QualType resType = UsualArithmeticConversions(lhsType, rhsType);
Steve Naroff5c10d4b2007-04-20 23:42:24 +0000741
Steve Naroff218bc2b2007-05-04 21:54:46 +0000742 if (resType->isArithmeticType())
743 return resType;
Steve Naroff6f49f5d2007-05-29 14:23:36 +0000744 InvalidOperands(loc, lex, rex);
Steve Naroff218bc2b2007-05-04 21:54:46 +0000745 return QualType();
Steve Naroff26c8ea52007-03-21 21:08:52 +0000746}
747
Steve Naroff218bc2b2007-05-04 21:54:46 +0000748inline QualType Sema::CheckRemainderOperands(
749 Expr *lex, Expr *rex, SourceLocation loc)
750{
Steve Naroffd6fbee82007-06-13 15:42:33 +0000751 QualType lhsType = lex->getType(), rhsType = rex->getType();
752 QualType resType = UsualArithmeticConversions(lhsType, rhsType);
Steve Naroff218bc2b2007-05-04 21:54:46 +0000753
754 if (resType->isIntegerType())
755 return resType;
Steve Naroff6f49f5d2007-05-29 14:23:36 +0000756 InvalidOperands(loc, lex, rex);
Steve Naroff218bc2b2007-05-04 21:54:46 +0000757 return QualType();
758}
759
760inline QualType Sema::CheckAdditionOperands( // C99 6.5.6
761 Expr *lex, Expr *rex, SourceLocation loc)
Steve Naroff1926c832007-04-24 00:23:05 +0000762{
Steve Naroffe4718892007-04-27 18:30:00 +0000763 QualType lhsType = lex->getType(), rhsType = rex->getType();
764 QualType resType = UsualArithmeticConversions(lhsType, rhsType);
765
766 // handle the common case first (both operands are arithmetic).
767 if (resType->isArithmeticType())
Steve Naroff218bc2b2007-05-04 21:54:46 +0000768 return resType;
769
770 if ((lhsType->isPointerType() && rhsType->isIntegerType()) ||
771 (lhsType->isIntegerType() && rhsType->isPointerType()))
772 return resType;
Steve Naroff6f49f5d2007-05-29 14:23:36 +0000773 InvalidOperands(loc, lex, rex);
Steve Naroff218bc2b2007-05-04 21:54:46 +0000774 return QualType();
Steve Naroff26c8ea52007-03-21 21:08:52 +0000775}
776
Steve Naroff218bc2b2007-05-04 21:54:46 +0000777inline QualType Sema::CheckSubtractionOperands( // C99 6.5.6
778 Expr *lex, Expr *rex, SourceLocation loc)
779{
780 QualType lhsType = lex->getType(), rhsType = rex->getType();
781 QualType resType = UsualArithmeticConversions(lhsType, rhsType);
782
783 // handle the common case first (both operands are arithmetic).
784 if (resType->isArithmeticType())
785 return resType;
786 if ((lhsType->isPointerType() && rhsType->isIntegerType()) ||
787 (lhsType->isPointerType() && rhsType->isPointerType()))
788 return resType;
Steve Naroff6f49f5d2007-05-29 14:23:36 +0000789 InvalidOperands(loc, lex, rex);
Steve Naroff218bc2b2007-05-04 21:54:46 +0000790 return QualType();
791}
792
793inline QualType Sema::CheckShiftOperands( // C99 6.5.7
794 Expr *lex, Expr *rex, SourceLocation loc)
Steve Naroff1926c832007-04-24 00:23:05 +0000795{
Chris Lattner1d411a82007-06-05 20:52:21 +0000796 // FIXME: Shifts don't perform usual arithmetic conversions. This is wrong
797 // for int << longlong -> the result type should be int, not long long.
Steve Naroffd6fbee82007-06-13 15:42:33 +0000798 QualType lhsType = lex->getType(), rhsType = rex->getType();
799 QualType resType = UsualArithmeticConversions(lhsType, rhsType);
Steve Naroff1926c832007-04-24 00:23:05 +0000800
Steve Naroff218bc2b2007-05-04 21:54:46 +0000801 if (resType->isIntegerType())
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::CheckRelationalOperands( // C99 6.5.8
808 Expr *lex, Expr *rex, SourceLocation loc)
Steve Naroff1926c832007-04-24 00:23:05 +0000809{
Steve Naroffd6fbee82007-06-13 15:42:33 +0000810 QualType lType = UsualUnaryConversions(lex->getType());
811 QualType rType = UsualUnaryConversions(rex->getType());
Steve Naroff1926c832007-04-24 00:23:05 +0000812
813 if (lType->isRealType() && rType->isRealType())
Steve Naroff218bc2b2007-05-04 21:54:46 +0000814 return Context.IntTy;
Steve Naroffe4718892007-04-27 18:30:00 +0000815
Steve Naroff75c17232007-06-13 21:41:08 +0000816 if (lType->isPointerType()) {
817 if (rType->isPointerType())
818 return Context.IntTy;
819 if (rType->isIntegerType()) {
820 if (!rex->isNullPointerConstant())
821 Diag(loc, diag::ext_typecheck_comparison_of_pointer_integer,
822 lex->getSourceRange(), rex->getSourceRange());
823 return Context.IntTy; // the previous diagnostic is a GCC extension.
824 }
825 } else if (rType->isPointerType()) {
826 if (lType->isIntegerType()) {
827 if (!lex->isNullPointerConstant())
828 Diag(loc, diag::ext_typecheck_comparison_of_pointer_integer,
829 lex->getSourceRange(), rex->getSourceRange());
830 return Context.IntTy; // the previous diagnostic is a GCC extension.
831 }
Steve Naroff043d45d2007-05-15 02:32:35 +0000832 }
Steve Naroff6f49f5d2007-05-29 14:23:36 +0000833 InvalidOperands(loc, lex, rex);
Steve Naroff218bc2b2007-05-04 21:54:46 +0000834 return QualType();
Steve Naroff26c8ea52007-03-21 21:08:52 +0000835}
836
Steve Naroff218bc2b2007-05-04 21:54:46 +0000837inline QualType Sema::CheckEqualityOperands( // C99 6.5.9
838 Expr *lex, Expr *rex, SourceLocation loc)
Steve Naroff1926c832007-04-24 00:23:05 +0000839{
Steve Naroffd6fbee82007-06-13 15:42:33 +0000840 QualType lType = UsualUnaryConversions(lex->getType());
841 QualType rType = UsualUnaryConversions(rex->getType());
Steve Naroff1926c832007-04-24 00:23:05 +0000842
843 if (lType->isArithmeticType() && rType->isArithmeticType())
Steve Naroff218bc2b2007-05-04 21:54:46 +0000844 return Context.IntTy;
Steve Naroff218bc2b2007-05-04 21:54:46 +0000845
Steve Naroff75c17232007-06-13 21:41:08 +0000846 if (lType->isPointerType()) {
847 if (rType->isPointerType())
848 return Context.IntTy;
849 if (rType->isIntegerType()) {
850 if (!rex->isNullPointerConstant())
851 Diag(loc, diag::ext_typecheck_comparison_of_pointer_integer,
852 lex->getSourceRange(), rex->getSourceRange());
853 return Context.IntTy; // the previous diagnostic is a GCC extension.
854 }
855 } else if (rType->isPointerType()) {
856 if (lType->isIntegerType()) {
857 if (!lex->isNullPointerConstant())
858 Diag(loc, diag::ext_typecheck_comparison_of_pointer_integer,
859 lex->getSourceRange(), rex->getSourceRange());
860 return Context.IntTy; // the previous diagnostic is a GCC extension.
861 }
Steve Naroff043d45d2007-05-15 02:32:35 +0000862 }
Steve Naroff6f49f5d2007-05-29 14:23:36 +0000863 InvalidOperands(loc, lex, rex);
Steve Naroff218bc2b2007-05-04 21:54:46 +0000864 return QualType();
Steve Naroff26c8ea52007-03-21 21:08:52 +0000865}
866
Steve Naroff218bc2b2007-05-04 21:54:46 +0000867inline QualType Sema::CheckBitwiseOperands(
868 Expr *lex, Expr *rex, SourceLocation loc)
Steve Naroff1926c832007-04-24 00:23:05 +0000869{
Steve Naroffd6fbee82007-06-13 15:42:33 +0000870 QualType lhsType = lex->getType(), rhsType = rex->getType();
871 QualType resType = UsualArithmeticConversions(lhsType, rhsType);
Steve Naroff1926c832007-04-24 00:23:05 +0000872
Steve Naroff218bc2b2007-05-04 21:54:46 +0000873 if (resType->isIntegerType())
874 return resType;
Steve Naroff6f49f5d2007-05-29 14:23:36 +0000875 InvalidOperands(loc, lex, rex);
Steve Naroff218bc2b2007-05-04 21:54:46 +0000876 return QualType();
Steve Naroff26c8ea52007-03-21 21:08:52 +0000877}
878
Steve Naroff218bc2b2007-05-04 21:54:46 +0000879inline QualType Sema::CheckLogicalOperands( // C99 6.5.[13,14]
880 Expr *lex, Expr *rex, SourceLocation loc)
Steve Naroff1926c832007-04-24 00:23:05 +0000881{
Steve Naroff71b59a92007-06-04 22:22:31 +0000882 QualType lhsType = UsualUnaryConversions(lex->getType());
883 QualType rhsType = UsualUnaryConversions(rex->getType());
Steve Naroffe4718892007-04-27 18:30:00 +0000884
Steve Naroff218bc2b2007-05-04 21:54:46 +0000885 if (lhsType->isScalarType() || rhsType->isScalarType())
886 return Context.IntTy;
Steve Naroff6f49f5d2007-05-29 14:23:36 +0000887 InvalidOperands(loc, lex, rex);
Steve Naroff218bc2b2007-05-04 21:54:46 +0000888 return QualType();
Steve Naroffae4143e2007-04-26 20:39:23 +0000889}
890
Steve Naroff35d85152007-05-07 00:24:15 +0000891inline QualType Sema::CheckAssignmentOperands( // C99 6.5.16.1
892 Expr *lex, Expr *rex, SourceLocation loc, QualType compoundType)
Steve Naroffae4143e2007-04-26 20:39:23 +0000893{
Steve Naroff0af91202007-04-27 21:51:21 +0000894 QualType lhsType = lex->getType();
Steve Naroff35d85152007-05-07 00:24:15 +0000895 QualType rhsType = compoundType.isNull() ? rex->getType() : compoundType;
Steve Naroff1f4d7272007-05-11 04:00:31 +0000896 bool hadError = false;
Steve Naroff9358c712007-05-27 23:58:33 +0000897 Expr::isModifiableLvalueResult mlval = lex->isModifiableLvalue();
898
899 switch (mlval) { // C99 6.5.16p2
900 case Expr::MLV_Valid:
901 break;
902 case Expr::MLV_ConstQualified:
903 Diag(loc, diag::err_typecheck_assign_const, lex->getSourceRange());
904 hadError = true;
905 break;
906 case Expr::MLV_ArrayType:
907 Diag(loc, diag::err_typecheck_array_not_modifiable_lvalue,
908 lhsType.getAsString(), lex->getSourceRange());
909 return QualType();
910 case Expr::MLV_NotObjectType:
911 Diag(loc, diag::err_typecheck_non_object_not_modifiable_lvalue,
912 lhsType.getAsString(), lex->getSourceRange());
913 return QualType();
914 case Expr::MLV_InvalidExpression:
915 Diag(loc, diag::err_typecheck_expression_not_modifiable_lvalue,
916 lex->getSourceRange());
917 return QualType();
918 case Expr::MLV_IncompleteType:
919 case Expr::MLV_IncompleteVoidType:
920 Diag(loc, diag::err_typecheck_incomplete_type_not_modifiable_lvalue,
921 lhsType.getAsString(), lex->getSourceRange());
922 return QualType();
Steve Naroff218bc2b2007-05-04 21:54:46 +0000923 }
924 if (lhsType == rhsType) // common case, fast path...
925 return lhsType;
926
Steve Naroff98cf3e92007-06-06 18:38:38 +0000927 AssignmentCheckResult result = CheckAssignmentConstraints(lhsType, rhsType);
Steve Naroff218bc2b2007-05-04 21:54:46 +0000928
929 // decode the result (notice that extensions still return a type).
930 switch (result) {
931 case Compatible:
Steve Naroff1f4d7272007-05-11 04:00:31 +0000932 break;
Steve Naroff218bc2b2007-05-04 21:54:46 +0000933 case Incompatible:
Steve Naroffe845e272007-05-18 01:06:45 +0000934 Diag(loc, diag::err_typecheck_assign_incompatible,
Chris Lattner84e160a2007-05-19 07:03:17 +0000935 lhsType.getAsString(), rhsType.getAsString(),
936 lex->getSourceRange(), rex->getSourceRange());
Steve Naroff1f4d7272007-05-11 04:00:31 +0000937 hadError = true;
938 break;
Steve Naroff218bc2b2007-05-04 21:54:46 +0000939 case PointerFromInt:
940 // check for null pointer constant (C99 6.3.2.3p3)
Steve Naroff9992bba2007-05-30 16:27:15 +0000941 if (compoundType.isNull() && !rex->isNullPointerConstant()) {
Steve Naroff56faab22007-05-30 04:20:12 +0000942 Diag(loc, diag::ext_typecheck_assign_pointer_int,
943 lhsType.getAsString(), rhsType.getAsString(),
Steve Naroff9358c712007-05-27 23:58:33 +0000944 lex->getSourceRange(), rex->getSourceRange());
Steve Naroff9992bba2007-05-30 16:27:15 +0000945 }
Steve Naroff1f4d7272007-05-11 04:00:31 +0000946 break;
Steve Naroff56faab22007-05-30 04:20:12 +0000947 case IntFromPointer:
948 Diag(loc, diag::ext_typecheck_assign_pointer_int,
949 lhsType.getAsString(), rhsType.getAsString(),
Steve Naroff9358c712007-05-27 23:58:33 +0000950 lex->getSourceRange(), rex->getSourceRange());
Steve Naroff1f4d7272007-05-11 04:00:31 +0000951 break;
Steve Naroff218bc2b2007-05-04 21:54:46 +0000952 case IncompatiblePointer:
Steve Naroff9358c712007-05-27 23:58:33 +0000953 Diag(loc, diag::ext_typecheck_assign_incompatible_pointer,
954 lhsType.getAsString(), rhsType.getAsString(),
955 lex->getSourceRange(), rex->getSourceRange());
Steve Naroff1f4d7272007-05-11 04:00:31 +0000956 break;
957 case CompatiblePointerDiscardsQualifiers:
Steve Naroff9358c712007-05-27 23:58:33 +0000958 Diag(loc, diag::ext_typecheck_assign_discards_qualifiers,
959 lhsType.getAsString(), rhsType.getAsString(),
960 lex->getSourceRange(), rex->getSourceRange());
Steve Naroff1f4d7272007-05-11 04:00:31 +0000961 break;
Steve Naroff218bc2b2007-05-04 21:54:46 +0000962 }
Steve Naroff98cf3e92007-06-06 18:38:38 +0000963 // C99 6.5.16p3: The type of an assignment expression is the type of the
964 // left operand unless the left operand has qualified type, in which case
965 // it is the unqualified version of the type of the left operand.
966 // C99 6.5.16.1p2: In simple assignment, the value of the right operand
967 // is converted to the type of the assignment expression (above).
968 // C++ 5.17p1: the type of the assignment expression is that of its left oprdu.
969 return hadError ? QualType() : lhsType.getUnqualifiedType();
Steve Naroffae4143e2007-04-26 20:39:23 +0000970}
971
Steve Naroff218bc2b2007-05-04 21:54:46 +0000972inline QualType Sema::CheckCommaOperands( // C99 6.5.17
Chris Lattner84e160a2007-05-19 07:03:17 +0000973 Expr *lex, Expr *rex, SourceLocation loc) {
Steve Naroff71b59a92007-06-04 22:22:31 +0000974 return UsualUnaryConversions(rex->getType());
Steve Naroff95af0132007-03-30 23:47:58 +0000975}
976
Steve Naroff71ce2e02007-05-18 22:53:50 +0000977QualType Sema::CheckIncrementDecrementOperand(Expr *op, SourceLocation OpLoc) {
Steve Naroffd6fbee82007-06-13 15:42:33 +0000978 QualType lhsType = op->getType(), rhsType = Context.IntTy;
979 QualType resType = UsualArithmeticConversions(lhsType, rhsType);
Steve Naroff35d85152007-05-07 00:24:15 +0000980 assert(!resType.isNull() && "no type for increment/decrement expression");
Steve Naroffd50c88e2007-04-05 21:15:20 +0000981
Steve Naroff46ba1eb2007-04-03 23:13:13 +0000982 // C99 6.5.2.4p1
Steve Naroff35d85152007-05-07 00:24:15 +0000983 if (const PointerType *pt = dyn_cast<PointerType>(resType)) {
984 if (!pt->getPointeeType()->isObjectType()) { // C99 6.5.2.4p2, 6.5.6p2
Steve Naroff71ce2e02007-05-18 22:53:50 +0000985 Diag(OpLoc, diag::err_typecheck_arithmetic_incomplete_type,
986 resType.getAsString(), op->getSourceRange());
Steve Naroff35d85152007-05-07 00:24:15 +0000987 return QualType();
988 }
989 } else if (!resType->isRealType()) {
Steve Naroff95af0132007-03-30 23:47:58 +0000990 // FIXME: Allow Complex as a GCC extension.
Steve Naroff71ce2e02007-05-18 22:53:50 +0000991 Diag(OpLoc, diag::err_typecheck_illegal_increment_decrement,
992 resType.getAsString(), op->getSourceRange());
Steve Naroff35d85152007-05-07 00:24:15 +0000993 return QualType();
Steve Naroff46ba1eb2007-04-03 23:13:13 +0000994 }
Steve Naroff094046f2007-05-13 03:21:25 +0000995 // At this point, we know we have a real or pointer type. Now make sure
996 // the operand is a modifiable lvalue.
Steve Naroff9358c712007-05-27 23:58:33 +0000997 Expr::isModifiableLvalueResult mlval = op->isModifiableLvalue();
998 if (mlval != Expr::MLV_Valid) {
999 // FIXME: emit a more precise diagnostic...
Steve Naroff71ce2e02007-05-18 22:53:50 +00001000 Diag(OpLoc, diag::err_typecheck_invalid_lvalue_incr_decr,
Chris Lattner84e160a2007-05-19 07:03:17 +00001001 op->getSourceRange());
Steve Naroff35d85152007-05-07 00:24:15 +00001002 return QualType();
1003 }
1004 return resType;
Steve Naroff26c8ea52007-03-21 21:08:52 +00001005}
1006
Steve Naroff1926c832007-04-24 00:23:05 +00001007/// getPrimaryDeclaration - Helper function for CheckAddressOfOperand().
Steve Naroff47500512007-04-19 23:00:49 +00001008/// This routine allows us to typecheck complex/recursive expressions
1009/// where the declaration is needed for type checking. Here are some
1010/// examples: &s.xx, &s.zz[1].yy, &(1+2), &(XX), &"123"[2].
Steve Naroff1926c832007-04-24 00:23:05 +00001011static Decl *getPrimaryDeclaration(Expr *e) {
Steve Naroff47500512007-04-19 23:00:49 +00001012 switch (e->getStmtClass()) {
1013 case Stmt::DeclRefExprClass:
1014 return cast<DeclRefExpr>(e)->getDecl();
1015 case Stmt::MemberExprClass:
Steve Naroff1926c832007-04-24 00:23:05 +00001016 return getPrimaryDeclaration(cast<MemberExpr>(e)->getBase());
Steve Naroff47500512007-04-19 23:00:49 +00001017 case Stmt::ArraySubscriptExprClass:
Steve Naroff1926c832007-04-24 00:23:05 +00001018 return getPrimaryDeclaration(cast<ArraySubscriptExpr>(e)->getBase());
Steve Naroff47500512007-04-19 23:00:49 +00001019 case Stmt::CallExprClass:
Steve Naroff1926c832007-04-24 00:23:05 +00001020 return getPrimaryDeclaration(cast<CallExpr>(e)->getCallee());
Steve Naroff47500512007-04-19 23:00:49 +00001021 case Stmt::UnaryOperatorClass:
Steve Naroff1926c832007-04-24 00:23:05 +00001022 return getPrimaryDeclaration(cast<UnaryOperator>(e)->getSubExpr());
Steve Naroff47500512007-04-19 23:00:49 +00001023 case Stmt::ParenExprClass:
Steve Naroff1926c832007-04-24 00:23:05 +00001024 return getPrimaryDeclaration(cast<ParenExpr>(e)->getSubExpr());
Steve Naroff47500512007-04-19 23:00:49 +00001025 default:
1026 return 0;
1027 }
1028}
1029
1030/// CheckAddressOfOperand - The operand of & must be either a function
1031/// designator or an lvalue designating an object. If it is an lvalue, the
1032/// object cannot be declared with storage class register or be a bit field.
1033/// Note: The usual conversions are *not* applied to the operand of the &
Steve Naroffa78fe7e2007-05-16 19:47:19 +00001034/// operator (C99 6.3.2.1p[2-4]), and its result is never an lvalue.
Steve Naroff35d85152007-05-07 00:24:15 +00001035QualType Sema::CheckAddressOfOperand(Expr *op, SourceLocation OpLoc) {
Steve Naroff1926c832007-04-24 00:23:05 +00001036 Decl *dcl = getPrimaryDeclaration(op);
Steve Naroff9358c712007-05-27 23:58:33 +00001037 Expr::isLvalueResult lval = op->isLvalue();
Steve Naroff47500512007-04-19 23:00:49 +00001038
Steve Naroff9358c712007-05-27 23:58:33 +00001039 if (lval != Expr::LV_Valid) { // C99 6.5.3.2p1
Steve Naroff5dd642e2007-05-14 18:14:51 +00001040 if (dcl && isa<FunctionDecl>(dcl)) // allow function designators
1041 ;
Steve Naroff9358c712007-05-27 23:58:33 +00001042 else { // FIXME: emit more specific diag...
Steve Naroff71ce2e02007-05-18 22:53:50 +00001043 Diag(OpLoc, diag::err_typecheck_invalid_lvalue_addrof,
1044 op->getSourceRange());
Steve Naroff35d85152007-05-07 00:24:15 +00001045 return QualType();
1046 }
Steve Naroff47500512007-04-19 23:00:49 +00001047 } else if (dcl) {
1048 // We have an lvalue with a decl. Make sure the decl is not declared
1049 // with the register storage-class specifier.
1050 if (const VarDecl *vd = dyn_cast<VarDecl>(dcl)) {
Steve Naroff35d85152007-05-07 00:24:15 +00001051 if (vd->getStorageClass() == VarDecl::Register) {
Steve Naroff71ce2e02007-05-18 22:53:50 +00001052 Diag(OpLoc, diag::err_typecheck_address_of_register,
Chris Lattner84e160a2007-05-19 07:03:17 +00001053 op->getSourceRange());
Steve Naroff35d85152007-05-07 00:24:15 +00001054 return QualType();
1055 }
Steve Narofff633d092007-04-25 19:01:39 +00001056 } else
1057 assert(0 && "Unknown/unexpected decl type");
1058
Steve Naroff47500512007-04-19 23:00:49 +00001059 // FIXME: add check for bitfields!
1060 }
1061 // If the operand has type "type", the result has type "pointer to type".
Steve Naroff35d85152007-05-07 00:24:15 +00001062 return Context.getPointerType(op->getType());
Steve Naroff47500512007-04-19 23:00:49 +00001063}
1064
Steve Naroff35d85152007-05-07 00:24:15 +00001065QualType Sema::CheckIndirectionOperand(Expr *op, SourceLocation OpLoc) {
Steve Naroff71b59a92007-06-04 22:22:31 +00001066 QualType qType = UsualUnaryConversions(op->getType());
Steve Naroff35d85152007-05-07 00:24:15 +00001067
Steve Naroff47500512007-04-19 23:00:49 +00001068 assert(!qType.isNull() && "no type for * expression");
1069
Steve Naroff758ada12007-05-28 16:15:57 +00001070 if (PointerType *PT = dyn_cast<PointerType>(qType.getCanonicalType())) {
1071 QualType ptype = PT->getPointeeType();
1072 // C99 6.5.3.2p4. "if it points to an object,...".
1073 if (ptype->isIncompleteType()) { // An incomplete type is not an object
1074 // GCC compat: special case 'void *' (treat as warning).
1075 if (ptype->isVoidType()) {
1076 Diag(OpLoc, diag::ext_typecheck_deref_ptr_to_void,
Steve Naroffeb9da942007-05-30 00:06:37 +00001077 qType.getAsString(), op->getSourceRange());
Steve Naroff758ada12007-05-28 16:15:57 +00001078 } else {
1079 Diag(OpLoc, diag::err_typecheck_deref_incomplete_type,
Steve Naroffeb9da942007-05-30 00:06:37 +00001080 ptype.getAsString(), op->getSourceRange());
Steve Naroff758ada12007-05-28 16:15:57 +00001081 return QualType();
1082 }
1083 }
1084 return ptype;
1085 }
1086 Diag(OpLoc, diag::err_typecheck_indirection_requires_pointer,
Steve Naroffeb9da942007-05-30 00:06:37 +00001087 qType.getAsString(), op->getSourceRange());
Steve Naroff35d85152007-05-07 00:24:15 +00001088 return QualType();
Steve Naroff1926c832007-04-24 00:23:05 +00001089}
Steve Naroff218bc2b2007-05-04 21:54:46 +00001090
1091static inline BinaryOperator::Opcode ConvertTokenKindToBinaryOpcode(
1092 tok::TokenKind Kind) {
1093 BinaryOperator::Opcode Opc;
1094 switch (Kind) {
1095 default: assert(0 && "Unknown binop!");
1096 case tok::star: Opc = BinaryOperator::Mul; break;
1097 case tok::slash: Opc = BinaryOperator::Div; break;
1098 case tok::percent: Opc = BinaryOperator::Rem; break;
1099 case tok::plus: Opc = BinaryOperator::Add; break;
1100 case tok::minus: Opc = BinaryOperator::Sub; break;
1101 case tok::lessless: Opc = BinaryOperator::Shl; break;
1102 case tok::greatergreater: Opc = BinaryOperator::Shr; break;
1103 case tok::lessequal: Opc = BinaryOperator::LE; break;
1104 case tok::less: Opc = BinaryOperator::LT; break;
1105 case tok::greaterequal: Opc = BinaryOperator::GE; break;
1106 case tok::greater: Opc = BinaryOperator::GT; break;
1107 case tok::exclaimequal: Opc = BinaryOperator::NE; break;
1108 case tok::equalequal: Opc = BinaryOperator::EQ; break;
1109 case tok::amp: Opc = BinaryOperator::And; break;
1110 case tok::caret: Opc = BinaryOperator::Xor; break;
1111 case tok::pipe: Opc = BinaryOperator::Or; break;
1112 case tok::ampamp: Opc = BinaryOperator::LAnd; break;
1113 case tok::pipepipe: Opc = BinaryOperator::LOr; break;
1114 case tok::equal: Opc = BinaryOperator::Assign; break;
1115 case tok::starequal: Opc = BinaryOperator::MulAssign; break;
1116 case tok::slashequal: Opc = BinaryOperator::DivAssign; break;
1117 case tok::percentequal: Opc = BinaryOperator::RemAssign; break;
1118 case tok::plusequal: Opc = BinaryOperator::AddAssign; break;
1119 case tok::minusequal: Opc = BinaryOperator::SubAssign; break;
1120 case tok::lesslessequal: Opc = BinaryOperator::ShlAssign; break;
1121 case tok::greatergreaterequal: Opc = BinaryOperator::ShrAssign; break;
1122 case tok::ampequal: Opc = BinaryOperator::AndAssign; break;
1123 case tok::caretequal: Opc = BinaryOperator::XorAssign; break;
1124 case tok::pipeequal: Opc = BinaryOperator::OrAssign; break;
1125 case tok::comma: Opc = BinaryOperator::Comma; break;
1126 }
1127 return Opc;
1128}
1129
Steve Naroff35d85152007-05-07 00:24:15 +00001130static inline UnaryOperator::Opcode ConvertTokenKindToUnaryOpcode(
1131 tok::TokenKind Kind) {
1132 UnaryOperator::Opcode Opc;
1133 switch (Kind) {
1134 default: assert(0 && "Unknown unary op!");
1135 case tok::plusplus: Opc = UnaryOperator::PreInc; break;
1136 case tok::minusminus: Opc = UnaryOperator::PreDec; break;
1137 case tok::amp: Opc = UnaryOperator::AddrOf; break;
1138 case tok::star: Opc = UnaryOperator::Deref; break;
1139 case tok::plus: Opc = UnaryOperator::Plus; break;
1140 case tok::minus: Opc = UnaryOperator::Minus; break;
1141 case tok::tilde: Opc = UnaryOperator::Not; break;
1142 case tok::exclaim: Opc = UnaryOperator::LNot; break;
1143 case tok::kw_sizeof: Opc = UnaryOperator::SizeOf; break;
1144 case tok::kw___alignof: Opc = UnaryOperator::AlignOf; break;
1145 case tok::kw___real: Opc = UnaryOperator::Real; break;
1146 case tok::kw___imag: Opc = UnaryOperator::Imag; break;
Chris Lattnerd0f76512007-06-08 22:16:53 +00001147 case tok::kw___extension__: Opc = UnaryOperator::Extension; break;
Steve Naroff35d85152007-05-07 00:24:15 +00001148 }
1149 return Opc;
1150}
1151
Steve Naroff218bc2b2007-05-04 21:54:46 +00001152// Binary Operators. 'Tok' is the token for the operator.
1153Action::ExprResult Sema::ParseBinOp(SourceLocation TokLoc, tok::TokenKind Kind,
1154 ExprTy *LHS, ExprTy *RHS) {
1155 BinaryOperator::Opcode Opc = ConvertTokenKindToBinaryOpcode(Kind);
1156 Expr *lhs = (Expr *)LHS, *rhs = (Expr*)RHS;
1157
1158 assert((lhs != 0) && "ParseBinOp(): missing left expression");
1159 assert((rhs != 0) && "ParseBinOp(): missing right expression");
1160
Chris Lattner256c21b2007-06-28 03:53:10 +00001161 QualType ResultTy; // Result type of the binary operator.
1162 QualType CompTy; // Computation type for compound assignments (e.g. '+=')
Steve Naroff218bc2b2007-05-04 21:54:46 +00001163
1164 switch (Opc) {
1165 default:
1166 assert(0 && "Unknown binary expr!");
1167 case BinaryOperator::Assign:
Chris Lattner256c21b2007-06-28 03:53:10 +00001168 ResultTy = CheckAssignmentOperands(lhs, rhs, TokLoc, QualType());
Steve Naroff218bc2b2007-05-04 21:54:46 +00001169 break;
1170 case BinaryOperator::Mul:
1171 case BinaryOperator::Div:
Chris Lattner256c21b2007-06-28 03:53:10 +00001172 ResultTy = CheckMultiplyDivideOperands(lhs, rhs, TokLoc);
Steve Naroff218bc2b2007-05-04 21:54:46 +00001173 break;
1174 case BinaryOperator::Rem:
Chris Lattner256c21b2007-06-28 03:53:10 +00001175 ResultTy = CheckRemainderOperands(lhs, rhs, TokLoc);
Steve Naroff218bc2b2007-05-04 21:54:46 +00001176 break;
1177 case BinaryOperator::Add:
Chris Lattner256c21b2007-06-28 03:53:10 +00001178 ResultTy = CheckAdditionOperands(lhs, rhs, TokLoc);
Steve Naroff218bc2b2007-05-04 21:54:46 +00001179 break;
1180 case BinaryOperator::Sub:
Chris Lattner256c21b2007-06-28 03:53:10 +00001181 ResultTy = CheckSubtractionOperands(lhs, rhs, TokLoc);
Steve Naroff218bc2b2007-05-04 21:54:46 +00001182 break;
1183 case BinaryOperator::Shl:
1184 case BinaryOperator::Shr:
Chris Lattner256c21b2007-06-28 03:53:10 +00001185 ResultTy = CheckShiftOperands(lhs, rhs, TokLoc);
Steve Naroff218bc2b2007-05-04 21:54:46 +00001186 break;
1187 case BinaryOperator::LE:
1188 case BinaryOperator::LT:
1189 case BinaryOperator::GE:
1190 case BinaryOperator::GT:
Chris Lattner256c21b2007-06-28 03:53:10 +00001191 ResultTy = CheckRelationalOperands(lhs, rhs, TokLoc);
Steve Naroff218bc2b2007-05-04 21:54:46 +00001192 break;
1193 case BinaryOperator::EQ:
1194 case BinaryOperator::NE:
Chris Lattner256c21b2007-06-28 03:53:10 +00001195 ResultTy = CheckEqualityOperands(lhs, rhs, TokLoc);
Steve Naroff218bc2b2007-05-04 21:54:46 +00001196 break;
1197 case BinaryOperator::And:
1198 case BinaryOperator::Xor:
1199 case BinaryOperator::Or:
Chris Lattner256c21b2007-06-28 03:53:10 +00001200 ResultTy = CheckBitwiseOperands(lhs, rhs, TokLoc);
Steve Naroff218bc2b2007-05-04 21:54:46 +00001201 break;
1202 case BinaryOperator::LAnd:
1203 case BinaryOperator::LOr:
Chris Lattner256c21b2007-06-28 03:53:10 +00001204 ResultTy = CheckLogicalOperands(lhs, rhs, TokLoc);
Steve Naroff218bc2b2007-05-04 21:54:46 +00001205 break;
1206 case BinaryOperator::MulAssign:
1207 case BinaryOperator::DivAssign:
Chris Lattner256c21b2007-06-28 03:53:10 +00001208 CompTy = CheckMultiplyDivideOperands(lhs, rhs, TokLoc);
1209 if (!CompTy.isNull())
1210 ResultTy = CheckAssignmentOperands(lhs, rhs, TokLoc, CompTy);
Steve Naroff218bc2b2007-05-04 21:54:46 +00001211 break;
1212 case BinaryOperator::RemAssign:
Chris Lattner256c21b2007-06-28 03:53:10 +00001213 CompTy = CheckRemainderOperands(lhs, rhs, TokLoc);
1214 if (!CompTy.isNull())
1215 ResultTy = CheckAssignmentOperands(lhs, rhs, TokLoc, CompTy);
Steve Naroff218bc2b2007-05-04 21:54:46 +00001216 break;
1217 case BinaryOperator::AddAssign:
Chris Lattner256c21b2007-06-28 03:53:10 +00001218 CompTy = CheckAdditionOperands(lhs, rhs, TokLoc);
1219 if (!CompTy.isNull())
1220 ResultTy = CheckAssignmentOperands(lhs, rhs, TokLoc, CompTy);
Steve Naroff218bc2b2007-05-04 21:54:46 +00001221 break;
1222 case BinaryOperator::SubAssign:
Chris Lattner256c21b2007-06-28 03:53:10 +00001223 CompTy = CheckSubtractionOperands(lhs, rhs, TokLoc);
1224 if (!CompTy.isNull())
1225 ResultTy = CheckAssignmentOperands(lhs, rhs, TokLoc, CompTy);
Steve Naroff218bc2b2007-05-04 21:54:46 +00001226 break;
1227 case BinaryOperator::ShlAssign:
1228 case BinaryOperator::ShrAssign:
Chris Lattner256c21b2007-06-28 03:53:10 +00001229 CompTy = CheckShiftOperands(lhs, rhs, TokLoc);
1230 if (!CompTy.isNull())
1231 ResultTy = CheckAssignmentOperands(lhs, rhs, TokLoc, CompTy);
Steve Naroff218bc2b2007-05-04 21:54:46 +00001232 break;
1233 case BinaryOperator::AndAssign:
1234 case BinaryOperator::XorAssign:
1235 case BinaryOperator::OrAssign:
Chris Lattner256c21b2007-06-28 03:53:10 +00001236 CompTy = CheckBitwiseOperands(lhs, rhs, TokLoc);
1237 if (!CompTy.isNull())
1238 ResultTy = CheckAssignmentOperands(lhs, rhs, TokLoc, CompTy);
Steve Naroff218bc2b2007-05-04 21:54:46 +00001239 break;
1240 case BinaryOperator::Comma:
Chris Lattner256c21b2007-06-28 03:53:10 +00001241 ResultTy = CheckCommaOperands(lhs, rhs, TokLoc);
Steve Naroff218bc2b2007-05-04 21:54:46 +00001242 break;
1243 }
Chris Lattner256c21b2007-06-28 03:53:10 +00001244 if (ResultTy.isNull())
Steve Naroff218bc2b2007-05-04 21:54:46 +00001245 return true;
Chris Lattner256c21b2007-06-28 03:53:10 +00001246 if (CompTy.isNull())
1247 return new BinaryOperator(lhs, rhs, Opc, ResultTy);
1248 else
Chris Lattner9369a562007-06-29 16:31:29 +00001249 return new CompoundAssignOperator(lhs, rhs, Opc, ResultTy, CompTy);
Steve Naroff218bc2b2007-05-04 21:54:46 +00001250}
1251
Steve Naroff35d85152007-05-07 00:24:15 +00001252// Unary Operators. 'Tok' is the token for the operator.
1253Action::ExprResult Sema::ParseUnaryOp(SourceLocation OpLoc, tok::TokenKind Op,
Chris Lattner86554282007-06-08 22:32:33 +00001254 ExprTy *input) {
1255 Expr *Input = (Expr*)input;
Steve Naroff35d85152007-05-07 00:24:15 +00001256 UnaryOperator::Opcode Opc = ConvertTokenKindToUnaryOpcode(Op);
1257 QualType resultType;
1258 switch (Opc) {
1259 default:
1260 assert(0 && "Unimplemented unary expr!");
1261 case UnaryOperator::PreInc:
1262 case UnaryOperator::PreDec:
Chris Lattner86554282007-06-08 22:32:33 +00001263 resultType = CheckIncrementDecrementOperand(Input, OpLoc);
Steve Naroff35d85152007-05-07 00:24:15 +00001264 break;
1265 case UnaryOperator::AddrOf:
Chris Lattner86554282007-06-08 22:32:33 +00001266 resultType = CheckAddressOfOperand(Input, OpLoc);
Steve Naroff35d85152007-05-07 00:24:15 +00001267 break;
1268 case UnaryOperator::Deref:
Chris Lattner86554282007-06-08 22:32:33 +00001269 resultType = CheckIndirectionOperand(Input, OpLoc);
Steve Naroff35d85152007-05-07 00:24:15 +00001270 break;
1271 case UnaryOperator::Plus:
1272 case UnaryOperator::Minus:
Chris Lattner86554282007-06-08 22:32:33 +00001273 resultType = UsualUnaryConversions(Input->getType());
Steve Naroff35d85152007-05-07 00:24:15 +00001274 if (!resultType->isArithmeticType()) // C99 6.5.3.3p1
Chris Lattnerc04bd6a2007-05-16 18:09:54 +00001275 return Diag(OpLoc, diag::err_typecheck_unary_expr,
1276 resultType.getAsString());
Steve Naroff35d85152007-05-07 00:24:15 +00001277 break;
1278 case UnaryOperator::Not: // bitwise complement
Chris Lattner86554282007-06-08 22:32:33 +00001279 resultType = UsualUnaryConversions(Input->getType());
Steve Naroff35d85152007-05-07 00:24:15 +00001280 if (!resultType->isIntegerType()) // C99 6.5.3.3p1
Chris Lattnerc04bd6a2007-05-16 18:09:54 +00001281 return Diag(OpLoc, diag::err_typecheck_unary_expr,
1282 resultType.getAsString());
Steve Naroff35d85152007-05-07 00:24:15 +00001283 break;
1284 case UnaryOperator::LNot: // logical negation
Steve Naroff71b59a92007-06-04 22:22:31 +00001285 // Unlike +/-/~, integer promotions aren't done here (C99 6.5.3.3p5).
Chris Lattner86554282007-06-08 22:32:33 +00001286 resultType = DefaultFunctionArrayConversion(Input->getType());
Steve Naroff35d85152007-05-07 00:24:15 +00001287 if (!resultType->isScalarType()) // C99 6.5.3.3p1
Chris Lattnerc04bd6a2007-05-16 18:09:54 +00001288 return Diag(OpLoc, diag::err_typecheck_unary_expr,
1289 resultType.getAsString());
Chris Lattnerbe31ed82007-06-02 19:11:33 +00001290 // LNot always has type int. C99 6.5.3.3p5.
1291 resultType = Context.IntTy;
Steve Naroff35d85152007-05-07 00:24:15 +00001292 break;
1293 case UnaryOperator::SizeOf:
Chris Lattner86554282007-06-08 22:32:33 +00001294 resultType = CheckSizeOfAlignOfOperand(Input->getType(), OpLoc, true);
Steve Naroff043d45d2007-05-15 02:32:35 +00001295 break;
Steve Naroff35d85152007-05-07 00:24:15 +00001296 case UnaryOperator::AlignOf:
Chris Lattner86554282007-06-08 22:32:33 +00001297 resultType = CheckSizeOfAlignOfOperand(Input->getType(), OpLoc, false);
1298 break;
1299 case UnaryOperator::Extension:
1300 // FIXME: does __extension__ cause any promotions? I would think not.
1301 resultType = Input->getType();
Steve Naroff043d45d2007-05-15 02:32:35 +00001302 break;
Steve Naroff35d85152007-05-07 00:24:15 +00001303 }
1304 if (resultType.isNull())
1305 return true;
Chris Lattner86554282007-06-08 22:32:33 +00001306 return new UnaryOperator(Input, Opc, resultType, OpLoc);
Steve Naroff35d85152007-05-07 00:24:15 +00001307}
Chris Lattnereefa10e2007-05-28 06:56:27 +00001308
1309/// ParseAddrLabel - Parse the GNU address of label extension: "&&foo".
1310Sema::ExprResult Sema::ParseAddrLabel(SourceLocation OpLoc,
1311 SourceLocation LabLoc,
1312 IdentifierInfo *LabelII) {
1313 // Look up the record for this label identifier.
1314 LabelStmt *&LabelDecl = LabelMap[LabelII];
1315
1316 // If we haven't seen this label yet, create a forward reference.
1317 if (LabelDecl == 0)
1318 LabelDecl = new LabelStmt(LabLoc, LabelII, 0);
1319
1320 // Create the AST node. The address of a label always has type 'void*'.
1321 return new AddrLabel(OpLoc, LabLoc, LabelDecl,
1322 Context.getPointerType(Context.VoidTy));
1323}
1324