blob: d7a89865ea0edc06b63763940358e310bd42cbf7 [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"
Ted Kremenek43fb8b02007-11-25 00:58:00 +000015#include "SemaUtil.h"
Chris Lattnercb6a3822006-11-10 06:20:45 +000016#include "clang/AST/ASTContext.h"
Chris Lattner17ed4872006-11-20 04:58:19 +000017#include "clang/AST/Decl.h"
Steve Naroffc6814ea2007-10-02 20:01:56 +000018#include "clang/AST/DeclObjC.h"
Chris Lattner5b183d82006-11-10 05:03:26 +000019#include "clang/AST/Expr.h"
Steve Naroffd54978b2007-09-18 23:55:05 +000020#include "clang/Parse/DeclSpec.h"
Chris Lattner5b183d82006-11-10 05:03:26 +000021#include "clang/Lex/Preprocessor.h"
Steve Naroff09ef4742007-03-09 23:16:33 +000022#include "clang/Lex/LiteralSupport.h"
Steve Narofff2fb89e2007-03-13 20:29:44 +000023#include "clang/Basic/SourceManager.h"
Chris Lattner5b183d82006-11-10 05:03:26 +000024#include "clang/Basic/Diagnostic.h"
Chris Lattnerac18be92006-11-20 06:49:47 +000025#include "clang/Basic/LangOptions.h"
Chris Lattner5b183d82006-11-10 05:03:26 +000026#include "clang/Basic/TargetInfo.h"
27#include "llvm/ADT/SmallString.h"
Chris Lattnerb87b1b32007-08-10 20:18:51 +000028#include "llvm/ADT/StringExtras.h"
Chris Lattner5b183d82006-11-10 05:03:26 +000029using namespace clang;
30
Steve Naroff83895f72007-09-16 03:34:24 +000031/// ActOnStringLiteral - The specified tokens were lexed as pasted string
Chris Lattner5b183d82006-11-10 05:03:26 +000032/// fragments (e.g. "foo" "bar" L"baz"). The result string has to handle string
33/// concatenation ([C99 5.1.1.2, translation phase #6]), so it may come from
34/// multiple tokens. However, the common case is that StringToks points to one
35/// string.
36///
37Action::ExprResult
Steve Naroff83895f72007-09-16 03:34:24 +000038Sema::ActOnStringLiteral(const Token *StringToks, unsigned NumStringToks) {
Chris Lattner5b183d82006-11-10 05:03:26 +000039 assert(NumStringToks && "Must have at least one string!");
40
Steve Naroff4f88b312007-03-13 22:37:02 +000041 StringLiteralParser Literal(StringToks, NumStringToks, PP, Context.Target);
42 if (Literal.hadError)
43 return ExprResult(true);
Chris Lattner5b183d82006-11-10 05:03:26 +000044
Chris Lattner23b7eb62007-06-15 23:05:46 +000045 llvm::SmallVector<SourceLocation, 4> StringTokLocs;
Chris Lattner5b183d82006-11-10 05:03:26 +000046 for (unsigned i = 0; i != NumStringToks; ++i)
47 StringTokLocs.push_back(StringToks[i].getLocation());
Steve Narofff1e53692007-03-23 22:27:02 +000048
49 // FIXME: handle wchar_t
Anders Carlssoncbfc4b82007-10-15 02:50:23 +000050 QualType t;
51
52 if (Literal.Pascal)
53 t = Context.getPointerType(Context.UnsignedCharTy);
54 else
55 t = Context.getPointerType(Context.CharTy);
56
57 if (Literal.Pascal && Literal.GetStringLength() > 256)
58 return Diag(StringToks[0].getLocation(), diag::err_pascal_string_too_long,
59 SourceRange(StringToks[0].getLocation(),
60 StringToks[NumStringToks-1].getLocation()));
Steve Narofff1e53692007-03-23 22:27:02 +000061
Chris Lattner5b183d82006-11-10 05:03:26 +000062 // Pass &StringTokLocs[0], StringTokLocs.size() to factory!
Steve Naroff4f88b312007-03-13 22:37:02 +000063 return new StringLiteral(Literal.GetString(), Literal.GetStringLength(),
Anders Carlssoncbfc4b82007-10-15 02:50:23 +000064 Literal.AnyWide, t,
65 StringToks[0].getLocation(),
Steve Naroff53f07dc2007-05-17 21:49:33 +000066 StringToks[NumStringToks-1].getLocation());
Chris Lattner5b183d82006-11-10 05:03:26 +000067}
68
Chris Lattnere168f762006-11-10 05:29:30 +000069
Steve Naroff30d242c2007-09-15 18:49:24 +000070/// ActOnIdentifierExpr - The parser read an identifier in expression context,
Chris Lattnerac18be92006-11-20 06:49:47 +000071/// validate it per-C99 6.5.1. HasTrailingLParen indicates whether this
72/// identifier is used in an function call context.
Steve Naroff30d242c2007-09-15 18:49:24 +000073Sema::ExprResult Sema::ActOnIdentifierExpr(Scope *S, SourceLocation Loc,
Chris Lattnerac18be92006-11-20 06:49:47 +000074 IdentifierInfo &II,
75 bool HasTrailingLParen) {
Chris Lattner17ed4872006-11-20 04:58:19 +000076 // Could be enum-constant or decl.
Steve Naroff2f742082007-09-16 16:16:00 +000077 ScopedDecl *D = LookupScopedDecl(&II, Decl::IDNS_Ordinary, Loc, S);
Chris Lattner17ed4872006-11-20 04:58:19 +000078 if (D == 0) {
Bill Wendling4073ed52007-02-13 01:51:42 +000079 // Otherwise, this could be an implicitly declared function reference (legal
Chris Lattner9561a0b2007-01-28 08:20:04 +000080 // in C90, extension in C99).
Chris Lattnerac18be92006-11-20 06:49:47 +000081 if (HasTrailingLParen &&
82 // Not in C++.
Steve Narofff1e53692007-03-23 22:27:02 +000083 !getLangOptions().CPlusPlus)
Chris Lattnerac18be92006-11-20 06:49:47 +000084 D = ImplicitlyDefineFunction(Loc, II, S);
Steve Naroff92e30f82007-04-02 22:35:25 +000085 else {
Steve Naroffe46504b2007-11-12 14:29:37 +000086 if (CurMethodDecl) {
87 ObjcInterfaceDecl *IFace = CurMethodDecl->getClassInterface();
88 ObjcInterfaceDecl *clsDeclared;
Steve Narofff60782b2007-11-15 02:58:25 +000089 if (ObjcIvarDecl *IV = IFace->lookupInstanceVariable(&II, clsDeclared)) {
90 IdentifierInfo &II = Context.Idents.get("self");
91 ExprResult SelfExpr = ActOnIdentifierExpr(S, Loc, II, false);
92 return new ObjCIvarRefExpr(IV, IV->getType(), Loc,
93 static_cast<Expr*>(SelfExpr.Val), true, true);
94 }
Steve Naroffe46504b2007-11-12 14:29:37 +000095 }
Chris Lattnerac18be92006-11-20 06:49:47 +000096 // If this name wasn't predeclared and if this is not a function call,
97 // diagnose the problem.
Steve Narofff1e53692007-03-23 22:27:02 +000098 return Diag(Loc, diag::err_undeclared_var_use, II.getName());
Steve Naroff92e30f82007-04-02 22:35:25 +000099 }
Chris Lattner17ed4872006-11-20 04:58:19 +0000100 }
Steve Naroff7e6f7c22007-08-28 03:03:08 +0000101 if (ValueDecl *VD = dyn_cast<ValueDecl>(D)) {
Steve Naroffcf871f52007-08-28 18:45:29 +0000102 // Only create DeclRefExpr's for valid Decl's.
Steve Narofff93b6722007-08-28 20:14:24 +0000103 if (VD->isInvalidDecl())
Steve Naroff7e6f7c22007-08-28 03:03:08 +0000104 return true;
Steve Naroff509fe022007-05-17 01:16:00 +0000105 return new DeclRefExpr(VD, VD->getType(), Loc);
Steve Naroff7e6f7c22007-08-28 03:03:08 +0000106 }
Steve Naroff46ba1eb2007-04-03 23:13:13 +0000107 if (isa<TypedefDecl>(D))
Steve Narofff1e53692007-03-23 22:27:02 +0000108 return Diag(Loc, diag::err_unexpected_typedef, II.getName());
Fariborz Jahanianb31a2f22007-12-05 18:16:33 +0000109 if (isa<ObjcInterfaceDecl>(D))
110 return Diag(Loc, diag::err_unexpected_interface, II.getName());
Steve Narofff1e53692007-03-23 22:27:02 +0000111
112 assert(0 && "Invalid decl");
Chris Lattnerbb0ab462007-07-21 04:57:45 +0000113 abort();
Chris Lattner17ed4872006-11-20 04:58:19 +0000114}
Chris Lattnere168f762006-11-10 05:29:30 +0000115
Steve Naroff83895f72007-09-16 03:34:24 +0000116Sema::ExprResult Sema::ActOnPreDefinedExpr(SourceLocation Loc,
Anders Carlsson625bfc82007-07-21 05:21:51 +0000117 tok::TokenKind Kind) {
118 PreDefinedExpr::IdentType IT;
119
Chris Lattnere168f762006-11-10 05:29:30 +0000120 switch (Kind) {
121 default:
122 assert(0 && "Unknown simple primary expr!");
Chris Lattnere168f762006-11-10 05:29:30 +0000123 case tok::kw___func__: // primary-expression: __func__ [C99 6.4.2.2]
Anders Carlsson625bfc82007-07-21 05:21:51 +0000124 IT = PreDefinedExpr::Func;
125 break;
Chris Lattnere168f762006-11-10 05:29:30 +0000126 case tok::kw___FUNCTION__: // primary-expression: __FUNCTION__ [GNU]
Anders Carlsson625bfc82007-07-21 05:21:51 +0000127 IT = PreDefinedExpr::Function;
128 break;
Chris Lattnere168f762006-11-10 05:29:30 +0000129 case tok::kw___PRETTY_FUNCTION__: // primary-expression: __P..Y_F..N__ [GNU]
Anders Carlsson625bfc82007-07-21 05:21:51 +0000130 IT = PreDefinedExpr::PrettyFunction;
131 break;
Chris Lattnere168f762006-11-10 05:29:30 +0000132 }
Anders Carlsson625bfc82007-07-21 05:21:51 +0000133
134 // Pre-defined identifiers are always of type char *.
135 return new PreDefinedExpr(Loc, Context.getPointerType(Context.CharTy), IT);
Chris Lattnere168f762006-11-10 05:29:30 +0000136}
137
Steve Naroff83895f72007-09-16 03:34:24 +0000138Sema::ExprResult Sema::ActOnCharacterConstant(const Token &Tok) {
Chris Lattner23b7eb62007-06-15 23:05:46 +0000139 llvm::SmallString<16> CharBuffer;
Steve Naroffae4143e2007-04-26 20:39:23 +0000140 CharBuffer.resize(Tok.getLength());
141 const char *ThisTokBegin = &CharBuffer[0];
142 unsigned ActualLength = PP.getSpelling(Tok, ThisTokBegin);
143
144 CharLiteralParser Literal(ThisTokBegin, ThisTokBegin+ActualLength,
145 Tok.getLocation(), PP);
146 if (Literal.hadError())
147 return ExprResult(true);
Steve Naroff509fe022007-05-17 01:16:00 +0000148 return new CharacterLiteral(Literal.getValue(), Context.IntTy,
149 Tok.getLocation());
Steve Naroffae4143e2007-04-26 20:39:23 +0000150}
151
Steve Naroff83895f72007-09-16 03:34:24 +0000152Action::ExprResult Sema::ActOnNumericConstant(const Token &Tok) {
Steve Narofff2fb89e2007-03-13 20:29:44 +0000153 // fast path for a single digit (which is quite common). A single digit
154 // cannot have a trigraph, escaped newline, radix prefix, or type suffix.
155 if (Tok.getLength() == 1) {
156 const char *t = PP.getSourceManager().getCharacterData(Tok.getLocation());
Chris Lattner67ca9252007-05-21 01:08:44 +0000157
Chris Lattner9cf21c52007-09-04 02:45:27 +0000158 unsigned IntSize = static_cast<unsigned>(
159 Context.getTypeSize(Context.IntTy, Tok.getLocation()));
Chris Lattner23b7eb62007-06-15 23:05:46 +0000160 return ExprResult(new IntegerLiteral(llvm::APInt(IntSize, *t-'0'),
161 Context.IntTy,
Steve Naroff509fe022007-05-17 01:16:00 +0000162 Tok.getLocation()));
Steve Narofff2fb89e2007-03-13 20:29:44 +0000163 }
Chris Lattner23b7eb62007-06-15 23:05:46 +0000164 llvm::SmallString<512> IntegerBuffer;
Steve Naroff8160ea22007-03-06 01:09:46 +0000165 IntegerBuffer.resize(Tok.getLength());
166 const char *ThisTokBegin = &IntegerBuffer[0];
167
Chris Lattner67ca9252007-05-21 01:08:44 +0000168 // Get the spelling of the token, which eliminates trigraphs, etc.
Steve Naroff8160ea22007-03-06 01:09:46 +0000169 unsigned ActualLength = PP.getSpelling(Tok, ThisTokBegin);
Steve Naroff09ef4742007-03-09 23:16:33 +0000170 NumericLiteralParser Literal(ThisTokBegin, ThisTokBegin+ActualLength,
Steve Naroff451d8f162007-03-12 23:22:38 +0000171 Tok.getLocation(), PP);
Steve Narofff2fb89e2007-03-13 20:29:44 +0000172 if (Literal.hadError)
173 return ExprResult(true);
Chris Lattner67ca9252007-05-21 01:08:44 +0000174
Chris Lattner1c20a172007-08-26 03:42:43 +0000175 Expr *Res;
176
177 if (Literal.isFloatingLiteral()) {
Chris Lattnerec0a6d92007-09-22 18:29:59 +0000178 QualType Ty;
179 const llvm::fltSemantics *Format;
180 uint64_t Size; unsigned Align;
181
182 if (Literal.isFloat) {
183 Ty = Context.FloatTy;
Ted Kremenek1daa3cf2007-12-12 22:39:36 +0000184 Context.Target.getFloatInfo(Size, Align, Format,
185 Context.getFullLoc(Tok.getLocation()));
186
Chris Lattnerec0a6d92007-09-22 18:29:59 +0000187 } else if (Literal.isLong) {
188 Ty = Context.LongDoubleTy;
Ted Kremenek1daa3cf2007-12-12 22:39:36 +0000189 Context.Target.getLongDoubleInfo(Size, Align, Format,
190 Context.getFullLoc(Tok.getLocation()));
Chris Lattnerec0a6d92007-09-22 18:29:59 +0000191 } else {
192 Ty = Context.DoubleTy;
Ted Kremenek1daa3cf2007-12-12 22:39:36 +0000193 Context.Target.getDoubleInfo(Size, Align, Format,
194 Context.getFullLoc(Tok.getLocation()));
Chris Lattnerec0a6d92007-09-22 18:29:59 +0000195 }
196
Ted Kremenek3a2c9502007-11-29 00:56:49 +0000197 // isExact will be set by GetFloatValue().
198 bool isExact = false;
199
200 Res = new FloatingLiteral(Literal.GetFloatValue(*Format,&isExact), &isExact,
201 Ty, Tok.getLocation());
202
Chris Lattner1c20a172007-08-26 03:42:43 +0000203 } else if (!Literal.isIntegerLiteral()) {
204 return ExprResult(true);
205 } else {
Steve Naroffe5aa9be2007-04-05 22:36:20 +0000206 QualType t;
Chris Lattner67ca9252007-05-21 01:08:44 +0000207
Neil Boothac582c52007-08-29 22:00:19 +0000208 // long long is a C99 feature.
209 if (!getLangOptions().C99 && !getLangOptions().CPlusPlus0x &&
Neil Booth4a1ee052007-08-29 22:13:52 +0000210 Literal.isLongLong)
Neil Boothac582c52007-08-29 22:00:19 +0000211 Diag(Tok.getLocation(), diag::ext_longlong);
212
Chris Lattner67ca9252007-05-21 01:08:44 +0000213 // Get the value in the widest-possible width.
Ted Kremenek1daa3cf2007-12-12 22:39:36 +0000214 llvm::APInt ResultVal(Context.Target.getIntMaxTWidth(
215 Context.getFullLoc(Tok.getLocation())), 0);
Chris Lattner67ca9252007-05-21 01:08:44 +0000216
217 if (Literal.GetIntegerValue(ResultVal)) {
218 // If this value didn't fit into uintmax_t, warn and force to ull.
219 Diag(Tok.getLocation(), diag::warn_integer_too_large);
220 t = Context.UnsignedLongLongTy;
Chris Lattner4481b422007-07-14 01:29:45 +0000221 assert(Context.getTypeSize(t, Tok.getLocation()) ==
Chris Lattner67ca9252007-05-21 01:08:44 +0000222 ResultVal.getBitWidth() && "long long is not intmax_t?");
Steve Naroff09ef4742007-03-09 23:16:33 +0000223 } else {
Chris Lattner67ca9252007-05-21 01:08:44 +0000224 // If this value fits into a ULL, try to figure out what else it fits into
225 // according to the rules of C99 6.4.4.1p5.
226
227 // Octal, Hexadecimal, and integers with a U suffix are allowed to
228 // be an unsigned int.
229 bool AllowUnsigned = Literal.isUnsigned || Literal.getRadix() != 10;
230
231 // Check from smallest to largest, picking the smallest type we can.
Chris Lattner7b939cf2007-08-23 21:58:08 +0000232 if (!Literal.isLong && !Literal.isLongLong) {
233 // Are int/unsigned possibilities?
Chris Lattner9cf21c52007-09-04 02:45:27 +0000234 unsigned IntSize = static_cast<unsigned>(
235 Context.getTypeSize(Context.IntTy,Tok.getLocation()));
Chris Lattner67ca9252007-05-21 01:08:44 +0000236 // Does it fit in a unsigned int?
237 if (ResultVal.isIntN(IntSize)) {
238 // Does it fit in a signed int?
239 if (!Literal.isUnsigned && ResultVal[IntSize-1] == 0)
240 t = Context.IntTy;
241 else if (AllowUnsigned)
242 t = Context.UnsignedIntTy;
243 }
244
245 if (!t.isNull())
246 ResultVal.trunc(IntSize);
247 }
248
249 // Are long/unsigned long possibilities?
250 if (t.isNull() && !Literal.isLongLong) {
Chris Lattner9cf21c52007-09-04 02:45:27 +0000251 unsigned LongSize = static_cast<unsigned>(
252 Context.getTypeSize(Context.LongTy, Tok.getLocation()));
Chris Lattner67ca9252007-05-21 01:08:44 +0000253
254 // Does it fit in a unsigned long?
255 if (ResultVal.isIntN(LongSize)) {
256 // Does it fit in a signed long?
257 if (!Literal.isUnsigned && ResultVal[LongSize-1] == 0)
258 t = Context.LongTy;
259 else if (AllowUnsigned)
260 t = Context.UnsignedLongTy;
261 }
262 if (!t.isNull())
263 ResultVal.trunc(LongSize);
264 }
265
266 // Finally, check long long if needed.
267 if (t.isNull()) {
Chris Lattner9cf21c52007-09-04 02:45:27 +0000268 unsigned LongLongSize = static_cast<unsigned>(
269 Context.getTypeSize(Context.LongLongTy, Tok.getLocation()));
Chris Lattner67ca9252007-05-21 01:08:44 +0000270
271 // Does it fit in a unsigned long long?
272 if (ResultVal.isIntN(LongLongSize)) {
273 // Does it fit in a signed long long?
274 if (!Literal.isUnsigned && ResultVal[LongLongSize-1] == 0)
275 t = Context.LongLongTy;
276 else if (AllowUnsigned)
277 t = Context.UnsignedLongLongTy;
278 }
279 }
280
281 // If we still couldn't decide a type, we probably have something that
282 // does not fit in a signed long long, but has no U suffix.
283 if (t.isNull()) {
284 Diag(Tok.getLocation(), diag::warn_integer_too_large_for_signed);
285 t = Context.UnsignedLongLongTy;
286 }
Steve Naroff09ef4742007-03-09 23:16:33 +0000287 }
Chris Lattner67ca9252007-05-21 01:08:44 +0000288
Chris Lattner1c20a172007-08-26 03:42:43 +0000289 Res = new IntegerLiteral(ResultVal, t, Tok.getLocation());
Steve Naroff09ef4742007-03-09 23:16:33 +0000290 }
Chris Lattner1c20a172007-08-26 03:42:43 +0000291
292 // If this is an imaginary literal, create the ImaginaryLiteral wrapper.
293 if (Literal.isImaginary)
294 Res = new ImaginaryLiteral(Res, Context.getComplexType(Res->getType()));
295
296 return Res;
Chris Lattnere168f762006-11-10 05:29:30 +0000297}
298
Steve Naroff83895f72007-09-16 03:34:24 +0000299Action::ExprResult Sema::ActOnParenExpr(SourceLocation L, SourceLocation R,
Chris Lattnere168f762006-11-10 05:29:30 +0000300 ExprTy *Val) {
Steve Naroffae4143e2007-04-26 20:39:23 +0000301 Expr *e = (Expr *)Val;
Steve Naroff83895f72007-09-16 03:34:24 +0000302 assert((e != 0) && "ActOnParenExpr() missing expr");
Steve Naroff53f07dc2007-05-17 21:49:33 +0000303 return new ParenExpr(L, R, e);
Chris Lattnere168f762006-11-10 05:29:30 +0000304}
305
Steve Naroff71b59a92007-06-04 22:22:31 +0000306/// The UsualUnaryConversions() function is *not* called by this routine.
Steve Naroffa78fe7e2007-05-16 19:47:19 +0000307/// See C99 6.3.2.1p[2-4] for more details.
Steve Naroff043d45d2007-05-15 02:32:35 +0000308QualType Sema::CheckSizeOfAlignOfOperand(QualType exprType,
309 SourceLocation OpLoc, bool isSizeof) {
310 // C99 6.5.3.4p1:
311 if (isa<FunctionType>(exprType) && isSizeof)
312 // alignof(function) is allowed.
313 Diag(OpLoc, diag::ext_sizeof_function_type);
314 else if (exprType->isVoidType())
315 Diag(OpLoc, diag::ext_sizeof_void_type, isSizeof ? "sizeof" : "__alignof");
316 else if (exprType->isIncompleteType()) {
Steve Naroff043d45d2007-05-15 02:32:35 +0000317 Diag(OpLoc, isSizeof ? diag::err_sizeof_incomplete_type :
Chris Lattner3dc3d772007-05-16 18:07:12 +0000318 diag::err_alignof_incomplete_type,
319 exprType.getAsString());
Steve Naroff043d45d2007-05-15 02:32:35 +0000320 return QualType(); // error
321 }
322 // C99 6.5.3.4p4: the type (an unsigned integer type) is size_t.
323 return Context.getSizeType();
324}
325
Chris Lattnere168f762006-11-10 05:29:30 +0000326Action::ExprResult Sema::
Steve Naroff83895f72007-09-16 03:34:24 +0000327ActOnSizeOfAlignOfTypeExpr(SourceLocation OpLoc, bool isSizeof,
Steve Naroff509fe022007-05-17 01:16:00 +0000328 SourceLocation LPLoc, TypeTy *Ty,
329 SourceLocation RPLoc) {
Chris Lattner0d8b1a12006-11-20 04:34:45 +0000330 // If error parsing type, ignore.
331 if (Ty == 0) return true;
Chris Lattner6531c102007-01-23 22:29:49 +0000332
333 // Verify that this is a valid expression.
Steve Naroffe5aa9be2007-04-05 22:36:20 +0000334 QualType ArgTy = QualType::getFromOpaquePtr(Ty);
Chris Lattner6531c102007-01-23 22:29:49 +0000335
Steve Naroff043d45d2007-05-15 02:32:35 +0000336 QualType resultType = CheckSizeOfAlignOfOperand(ArgTy, OpLoc, isSizeof);
337
338 if (resultType.isNull())
339 return true;
Steve Naroff509fe022007-05-17 01:16:00 +0000340 return new SizeOfAlignOfTypeExpr(isSizeof, ArgTy, resultType, OpLoc, RPLoc);
Chris Lattnere168f762006-11-10 05:29:30 +0000341}
342
Chris Lattner74ed76b2007-08-24 21:41:10 +0000343QualType Sema::CheckRealImagOperand(Expr *&V, SourceLocation Loc) {
Chris Lattner30b5dd02007-08-24 21:16:53 +0000344 DefaultFunctionArrayConversion(V);
345
Chris Lattnere267f5d2007-08-26 05:39:26 +0000346 // These operators return the element type of a complex type.
Chris Lattner30b5dd02007-08-24 21:16:53 +0000347 if (const ComplexType *CT = V->getType()->getAsComplexType())
348 return CT->getElementType();
Chris Lattnere267f5d2007-08-26 05:39:26 +0000349
350 // Otherwise they pass through real integer and floating point types here.
351 if (V->getType()->isArithmeticType())
352 return V->getType();
353
354 // Reject anything else.
355 Diag(Loc, diag::err_realimag_invalid_type, V->getType().getAsString());
356 return QualType();
Chris Lattner30b5dd02007-08-24 21:16:53 +0000357}
358
359
Chris Lattnere168f762006-11-10 05:29:30 +0000360
Steve Naroff83895f72007-09-16 03:34:24 +0000361Action::ExprResult Sema::ActOnPostfixUnaryOp(SourceLocation OpLoc,
Chris Lattnere168f762006-11-10 05:29:30 +0000362 tok::TokenKind Kind,
363 ExprTy *Input) {
364 UnaryOperator::Opcode Opc;
365 switch (Kind) {
366 default: assert(0 && "Unknown unary op!");
367 case tok::plusplus: Opc = UnaryOperator::PostInc; break;
368 case tok::minusminus: Opc = UnaryOperator::PostDec; break;
369 }
Steve Naroff71ce2e02007-05-18 22:53:50 +0000370 QualType result = CheckIncrementDecrementOperand((Expr *)Input, OpLoc);
Steve Naroff35d85152007-05-07 00:24:15 +0000371 if (result.isNull())
372 return true;
Steve Naroff509fe022007-05-17 01:16:00 +0000373 return new UnaryOperator((Expr *)Input, Opc, result, OpLoc);
Chris Lattnere168f762006-11-10 05:29:30 +0000374}
375
376Action::ExprResult Sema::
Steve Naroff83895f72007-09-16 03:34:24 +0000377ActOnArraySubscriptExpr(ExprTy *Base, SourceLocation LLoc,
Chris Lattnere168f762006-11-10 05:29:30 +0000378 ExprTy *Idx, SourceLocation RLoc) {
Chris Lattner5981db42007-07-15 23:59:53 +0000379 Expr *LHSExp = static_cast<Expr*>(Base), *RHSExp = static_cast<Expr*>(Idx);
Chris Lattner36d572b2007-07-16 00:14:47 +0000380
381 // Perform default conversions.
382 DefaultFunctionArrayConversion(LHSExp);
383 DefaultFunctionArrayConversion(RHSExp);
Chris Lattner5981db42007-07-15 23:59:53 +0000384
Chris Lattner36d572b2007-07-16 00:14:47 +0000385 QualType LHSTy = LHSExp->getType(), RHSTy = RHSExp->getType();
Steve Narofff1e53692007-03-23 22:27:02 +0000386
Steve Naroffc1aadb12007-03-28 21:49:40 +0000387 // C99 6.5.2.1p2: the expression e1[e2] is by definition precisely equivalent
Chris Lattnerf17bd422007-08-30 17:45:32 +0000388 // to the expression *((e1)+(e2)). This means the array "Base" may actually be
Steve Narofff1e53692007-03-23 22:27:02 +0000389 // in the subscript position. As a result, we need to derive the array base
390 // and index from the expression types.
Chris Lattner36d572b2007-07-16 00:14:47 +0000391 Expr *BaseExpr, *IndexExpr;
392 QualType ResultType;
Chris Lattnerc996b172007-07-31 16:53:04 +0000393 if (const PointerType *PTy = LHSTy->getAsPointerType()) {
Chris Lattner36d572b2007-07-16 00:14:47 +0000394 BaseExpr = LHSExp;
395 IndexExpr = RHSExp;
396 // FIXME: need to deal with const...
397 ResultType = PTy->getPointeeType();
Chris Lattnerc996b172007-07-31 16:53:04 +0000398 } else if (const PointerType *PTy = RHSTy->getAsPointerType()) {
Chris Lattneraee0cfd2007-07-16 00:23:25 +0000399 // Handle the uncommon case of "123[Ptr]".
Chris Lattner36d572b2007-07-16 00:14:47 +0000400 BaseExpr = RHSExp;
401 IndexExpr = LHSExp;
402 // FIXME: need to deal with const...
403 ResultType = PTy->getPointeeType();
Chris Lattner41977962007-07-31 19:29:30 +0000404 } else if (const VectorType *VTy = LHSTy->getAsVectorType()) {
405 BaseExpr = LHSExp; // vectors: V[123]
Chris Lattner36d572b2007-07-16 00:14:47 +0000406 IndexExpr = RHSExp;
Steve Naroff01047312007-08-03 22:40:33 +0000407
408 // Component access limited to variables (reject vec4.rg[1]).
409 if (!isa<DeclRefExpr>(BaseExpr))
410 return Diag(LLoc, diag::err_ocuvector_component_access,
411 SourceRange(LLoc, RLoc));
Chris Lattner36d572b2007-07-16 00:14:47 +0000412 // FIXME: need to deal with const...
413 ResultType = VTy->getElementType();
Steve Naroffb3096442007-06-09 03:47:53 +0000414 } else {
Chris Lattner5981db42007-07-15 23:59:53 +0000415 return Diag(LHSExp->getLocStart(), diag::err_typecheck_subscript_value,
416 RHSExp->getSourceRange());
Steve Naroffb3096442007-06-09 03:47:53 +0000417 }
Steve Naroffc1aadb12007-03-28 21:49:40 +0000418 // C99 6.5.2.1p1
Chris Lattner36d572b2007-07-16 00:14:47 +0000419 if (!IndexExpr->getType()->isIntegerType())
420 return Diag(IndexExpr->getLocStart(), diag::err_typecheck_subscript,
421 IndexExpr->getSourceRange());
Steve Naroffb29cdd52007-07-10 18:23:31 +0000422
Chris Lattner36d572b2007-07-16 00:14:47 +0000423 // C99 6.5.2.1p1: "shall have type "pointer to *object* type". In practice,
424 // the following check catches trying to index a pointer to a function (e.g.
425 // void (*)(int)). Functions are not objects in C99.
426 if (!ResultType->isObjectType())
427 return Diag(BaseExpr->getLocStart(),
428 diag::err_typecheck_subscript_not_object,
429 BaseExpr->getType().getAsString(), BaseExpr->getSourceRange());
430
431 return new ArraySubscriptExpr(LHSExp, RHSExp, ResultType, RLoc);
Chris Lattnere168f762006-11-10 05:29:30 +0000432}
433
Steve Narofff8fd09e2007-07-27 22:15:19 +0000434QualType Sema::
435CheckOCUVectorComponent(QualType baseType, SourceLocation OpLoc,
436 IdentifierInfo &CompName, SourceLocation CompLoc) {
Chris Lattner41977962007-07-31 19:29:30 +0000437 const OCUVectorType *vecType = baseType->getAsOCUVectorType();
Steve Narofff8fd09e2007-07-27 22:15:19 +0000438
439 // The vector accessor can't exceed the number of elements.
440 const char *compStr = CompName.getName();
441 if (strlen(compStr) > vecType->getNumElements()) {
442 Diag(OpLoc, diag::err_ocuvector_component_exceeds_length,
443 baseType.getAsString(), SourceRange(CompLoc));
444 return QualType();
445 }
446 // The component names must come from the same set.
Chris Lattner7e152db2007-08-02 22:33:49 +0000447 if (vecType->getPointAccessorIdx(*compStr) != -1) {
448 do
449 compStr++;
450 while (*compStr && vecType->getPointAccessorIdx(*compStr) != -1);
451 } else if (vecType->getColorAccessorIdx(*compStr) != -1) {
452 do
453 compStr++;
454 while (*compStr && vecType->getColorAccessorIdx(*compStr) != -1);
455 } else if (vecType->getTextureAccessorIdx(*compStr) != -1) {
456 do
457 compStr++;
458 while (*compStr && vecType->getTextureAccessorIdx(*compStr) != -1);
459 }
Steve Narofff8fd09e2007-07-27 22:15:19 +0000460
461 if (*compStr) {
462 // We didn't get to the end of the string. This means the component names
463 // didn't come from the same set *or* we encountered an illegal name.
464 Diag(OpLoc, diag::err_ocuvector_component_name_illegal,
465 std::string(compStr,compStr+1), SourceRange(CompLoc));
466 return QualType();
467 }
468 // Each component accessor can't exceed the vector type.
469 compStr = CompName.getName();
470 while (*compStr) {
471 if (vecType->isAccessorWithinNumElements(*compStr))
472 compStr++;
473 else
474 break;
475 }
476 if (*compStr) {
477 // We didn't get to the end of the string. This means a component accessor
478 // exceeds the number of elements in the vector.
479 Diag(OpLoc, diag::err_ocuvector_component_exceeds_length,
480 baseType.getAsString(), SourceRange(CompLoc));
481 return QualType();
482 }
483 // The component accessor looks fine - now we need to compute the actual type.
484 // The vector type is implied by the component accessor. For example,
485 // vec4.b is a float, vec4.xy is a vec2, vec4.rgb is a vec3, etc.
486 unsigned CompSize = strlen(CompName.getName());
487 if (CompSize == 1)
488 return vecType->getElementType();
Steve Naroffddf5a1d2007-07-29 16:33:31 +0000489
490 QualType VT = Context.getOCUVectorType(vecType->getElementType(), CompSize);
491 // Now look up the TypeDefDecl from the vector type. Without this,
492 // diagostics look bad. We want OCU vector types to appear built-in.
493 for (unsigned i = 0, e = OCUVectorDecls.size(); i != e; ++i) {
494 if (OCUVectorDecls[i]->getUnderlyingType() == VT)
495 return Context.getTypedefType(OCUVectorDecls[i]);
496 }
497 return VT; // should never get here (a typedef type should always be found).
Steve Narofff8fd09e2007-07-27 22:15:19 +0000498}
499
Chris Lattnere168f762006-11-10 05:29:30 +0000500Action::ExprResult Sema::
Steve Naroff83895f72007-09-16 03:34:24 +0000501ActOnMemberReferenceExpr(ExprTy *Base, SourceLocation OpLoc,
Chris Lattnere168f762006-11-10 05:29:30 +0000502 tok::TokenKind OpKind, SourceLocation MemberLoc,
503 IdentifierInfo &Member) {
Steve Naroff185616f2007-07-26 03:11:44 +0000504 Expr *BaseExpr = static_cast<Expr *>(Base);
505 assert(BaseExpr && "no record expression");
Steve Naroffeaaae462007-12-16 21:42:28 +0000506
507 // Perform default conversions.
508 DefaultFunctionArrayConversion(BaseExpr);
Steve Naroffca8f7122007-04-01 01:41:35 +0000509
Steve Naroff185616f2007-07-26 03:11:44 +0000510 QualType BaseType = BaseExpr->getType();
511 assert(!BaseType.isNull() && "no type for member expression");
Steve Naroffca8f7122007-04-01 01:41:35 +0000512
Steve Narofff1e53692007-03-23 22:27:02 +0000513 if (OpKind == tok::arrow) {
Chris Lattnerc996b172007-07-31 16:53:04 +0000514 if (const PointerType *PT = BaseType->getAsPointerType())
Steve Naroff185616f2007-07-26 03:11:44 +0000515 BaseType = PT->getPointeeType();
516 else
517 return Diag(OpLoc, diag::err_typecheck_member_reference_arrow,
518 SourceRange(MemberLoc));
Steve Narofff1e53692007-03-23 22:27:02 +0000519 }
Steve Narofff8fd09e2007-07-27 22:15:19 +0000520 // The base type is either a record or an OCUVectorType.
Chris Lattner41977962007-07-31 19:29:30 +0000521 if (const RecordType *RTy = BaseType->getAsRecordType()) {
Steve Naroff185616f2007-07-26 03:11:44 +0000522 RecordDecl *RDecl = RTy->getDecl();
523 if (RTy->isIncompleteType())
524 return Diag(OpLoc, diag::err_typecheck_incomplete_tag, RDecl->getName(),
525 BaseExpr->getSourceRange());
526 // The record definition is complete, now make sure the member is valid.
Steve Narofff8fd09e2007-07-27 22:15:19 +0000527 FieldDecl *MemberDecl = RDecl->getMember(&Member);
528 if (!MemberDecl)
Steve Naroff185616f2007-07-26 03:11:44 +0000529 return Diag(OpLoc, diag::err_typecheck_no_member, Member.getName(),
530 SourceRange(MemberLoc));
Steve Narofff8fd09e2007-07-27 22:15:19 +0000531 return new MemberExpr(BaseExpr, OpKind==tok::arrow, MemberDecl, MemberLoc);
532 } else if (BaseType->isOCUVectorType() && OpKind == tok::period) {
Steve Naroff01047312007-08-03 22:40:33 +0000533 // Component access limited to variables (reject vec4.rg.g).
534 if (!isa<DeclRefExpr>(BaseExpr))
535 return Diag(OpLoc, diag::err_ocuvector_component_access,
536 SourceRange(MemberLoc));
Steve Narofff8fd09e2007-07-27 22:15:19 +0000537 QualType ret = CheckOCUVectorComponent(BaseType, OpLoc, Member, MemberLoc);
538 if (ret.isNull())
539 return true;
Chris Lattnerd268a7a2007-08-03 17:31:20 +0000540 return new OCUVectorElementExpr(ret, BaseExpr, Member, MemberLoc);
Fariborz Jahanian21f54ee2007-11-12 22:29:28 +0000541 } else if (BaseType->isObjcInterfaceType()) {
542 ObjcInterfaceDecl *IFace;
543 if (isa<ObjcInterfaceType>(BaseType.getCanonicalType()))
544 IFace = dyn_cast<ObjcInterfaceType>(BaseType)->getDecl();
545 else
Fariborz Jahanianc47dc4f2007-12-13 20:47:42 +0000546 IFace = dyn_cast<ObjcQualifiedInterfaceType>(BaseType)->getDecl();
Fariborz Jahanian21f54ee2007-11-12 22:29:28 +0000547 ObjcInterfaceDecl *clsDeclared;
548 if (ObjcIvarDecl *IV = IFace->lookupInstanceVariable(&Member, clsDeclared))
549 return new ObjCIvarRefExpr(IV, IV->getType(), MemberLoc, BaseExpr,
550 OpKind==tok::arrow);
551 }
552 return Diag(OpLoc, diag::err_typecheck_member_reference_structUnion,
553 SourceRange(MemberLoc));
Chris Lattnere168f762006-11-10 05:29:30 +0000554}
555
Steve Naroff83895f72007-09-16 03:34:24 +0000556/// ActOnCallExpr - Handle a call to Fn with the specified array of arguments.
Chris Lattnere168f762006-11-10 05:29:30 +0000557/// This provides the location of the left/right parens and a list of comma
558/// locations.
559Action::ExprResult Sema::
Steve Naroff83895f72007-09-16 03:34:24 +0000560ActOnCallExpr(ExprTy *fn, SourceLocation LParenLoc,
Chris Lattner38dbdb22007-07-21 03:03:59 +0000561 ExprTy **args, unsigned NumArgsInCall,
Chris Lattnere168f762006-11-10 05:29:30 +0000562 SourceLocation *CommaLocs, SourceLocation RParenLoc) {
Chris Lattner38dbdb22007-07-21 03:03:59 +0000563 Expr *Fn = static_cast<Expr *>(fn);
564 Expr **Args = reinterpret_cast<Expr**>(args);
565 assert(Fn && "no function call expression");
Steve Naroff8563f652007-05-28 19:25:56 +0000566
Chris Lattner38dbdb22007-07-21 03:03:59 +0000567 UsualUnaryConversions(Fn);
568 QualType funcType = Fn->getType();
Steve Naroffae4143e2007-04-26 20:39:23 +0000569
Chris Lattner3d01e4e2007-06-06 05:14:05 +0000570 // C99 6.5.2.2p1 - "The expression that denotes the called function shall have
571 // type pointer to function".
Chris Lattnere5a6cbd2007-07-31 21:27:01 +0000572 const PointerType *PT = funcType->getAsPointerType();
Chris Lattner3d01e4e2007-06-06 05:14:05 +0000573 if (PT == 0)
Chris Lattner38dbdb22007-07-21 03:03:59 +0000574 return Diag(Fn->getLocStart(), diag::err_typecheck_call_not_function,
575 SourceRange(Fn->getLocStart(), RParenLoc));
Chris Lattner3343f812007-06-06 05:05:41 +0000576
Chris Lattnere5a6cbd2007-07-31 21:27:01 +0000577 const FunctionType *funcT = PT->getPointeeType()->getAsFunctionType();
Chris Lattner3d01e4e2007-06-06 05:14:05 +0000578 if (funcT == 0)
Chris Lattner38dbdb22007-07-21 03:03:59 +0000579 return Diag(Fn->getLocStart(), diag::err_typecheck_call_not_function,
580 SourceRange(Fn->getLocStart(), RParenLoc));
Steve Naroffb8c289d2007-05-08 22:18:00 +0000581
Steve Naroff17f76e02007-05-03 21:03:48 +0000582 // If a prototype isn't declared, the parser implicitly defines a func decl
583 QualType resultType = funcT->getResultType();
584
585 if (const FunctionTypeProto *proto = dyn_cast<FunctionTypeProto>(funcT)) {
586 // C99 6.5.2.2p7 - the arguments are implicitly converted, as if by
587 // assignment, to the types of the corresponding parameter, ...
588
589 unsigned NumArgsInProto = proto->getNumArgs();
Steve Naroffb8c289d2007-05-08 22:18:00 +0000590 unsigned NumArgsToCheck = NumArgsInCall;
Steve Naroff17f76e02007-05-03 21:03:48 +0000591
Steve Naroffb8c289d2007-05-08 22:18:00 +0000592 if (NumArgsInCall < NumArgsInProto)
Steve Naroff56faab22007-05-30 04:20:12 +0000593 Diag(RParenLoc, diag::err_typecheck_call_too_few_args,
Chris Lattner38dbdb22007-07-21 03:03:59 +0000594 Fn->getSourceRange());
Steve Naroffb8c289d2007-05-08 22:18:00 +0000595 else if (NumArgsInCall > NumArgsInProto) {
Steve Naroff8563f652007-05-28 19:25:56 +0000596 if (!proto->isVariadic()) {
Chris Lattnera6f5ab52007-07-21 03:09:58 +0000597 Diag(Args[NumArgsInProto]->getLocStart(),
Chris Lattner38dbdb22007-07-21 03:03:59 +0000598 diag::err_typecheck_call_too_many_args, Fn->getSourceRange(),
Chris Lattnera6f5ab52007-07-21 03:09:58 +0000599 SourceRange(Args[NumArgsInProto]->getLocStart(),
600 Args[NumArgsInCall-1]->getLocEnd()));
Steve Naroff8563f652007-05-28 19:25:56 +0000601 }
Steve Naroffb8c289d2007-05-08 22:18:00 +0000602 NumArgsToCheck = NumArgsInProto;
Steve Naroff17f76e02007-05-03 21:03:48 +0000603 }
604 // Continue to check argument types (even if we have too few/many args).
Steve Naroffb8c289d2007-05-08 22:18:00 +0000605 for (unsigned i = 0; i < NumArgsToCheck; i++) {
Chris Lattner38dbdb22007-07-21 03:03:59 +0000606 Expr *argExpr = Args[i];
Steve Naroff83895f72007-09-16 03:34:24 +0000607 assert(argExpr && "ActOnCallExpr(): missing argument expression");
Steve Naroff8563f652007-05-28 19:25:56 +0000608
Steve Naroff17f76e02007-05-03 21:03:48 +0000609 QualType lhsType = proto->getArgType(i);
Steve Naroff8563f652007-05-28 19:25:56 +0000610 QualType rhsType = argExpr->getType();
Steve Naroff44fd8ff2007-07-24 21:46:40 +0000611
Steve Naroffb8af1c22007-07-25 20:45:33 +0000612 // If necessary, apply function/array conversion. C99 6.7.5.3p[7,8].
Chris Lattner41977962007-07-31 19:29:30 +0000613 if (const ArrayType *ary = lhsType->getAsArrayType())
Steve Naroff44fd8ff2007-07-24 21:46:40 +0000614 lhsType = Context.getPointerType(ary->getElementType());
Steve Naroffb8af1c22007-07-25 20:45:33 +0000615 else if (lhsType->isFunctionType())
Steve Naroff44fd8ff2007-07-24 21:46:40 +0000616 lhsType = Context.getPointerType(lhsType);
617
Steve Naroffb8ea4fb2007-07-13 23:32:42 +0000618 AssignmentCheckResult result = CheckSingleAssignmentConstraints(lhsType,
619 argExpr);
Steve Naroff0c1c7ed2007-08-24 22:33:52 +0000620 if (Args[i] != argExpr) // The expression was converted.
621 Args[i] = argExpr; // Make sure we store the converted expression.
Steve Naroff6f49f5d2007-05-29 14:23:36 +0000622 SourceLocation l = argExpr->getLocStart();
Steve Naroff17f76e02007-05-03 21:03:48 +0000623
624 // decode the result (notice that AST's are still created for extensions).
Steve Naroff17f76e02007-05-03 21:03:48 +0000625 switch (result) {
626 case Compatible:
627 break;
628 case PointerFromInt:
Steve Naroff0ee0b0a2007-11-27 17:58:44 +0000629 Diag(l, diag::ext_typecheck_passing_pointer_int,
630 lhsType.getAsString(), rhsType.getAsString(),
631 Fn->getSourceRange(), argExpr->getSourceRange());
Steve Naroff17f76e02007-05-03 21:03:48 +0000632 break;
633 case IntFromPointer:
Steve Naroff56faab22007-05-30 04:20:12 +0000634 Diag(l, diag::ext_typecheck_passing_pointer_int,
635 lhsType.getAsString(), rhsType.getAsString(),
Chris Lattner38dbdb22007-07-21 03:03:59 +0000636 Fn->getSourceRange(), argExpr->getSourceRange());
Steve Naroff17f76e02007-05-03 21:03:48 +0000637 break;
638 case IncompatiblePointer:
Steve Naroff8563f652007-05-28 19:25:56 +0000639 Diag(l, diag::ext_typecheck_passing_incompatible_pointer,
Steve Naroffeb9da942007-05-30 00:06:37 +0000640 rhsType.getAsString(), lhsType.getAsString(),
Chris Lattner38dbdb22007-07-21 03:03:59 +0000641 Fn->getSourceRange(), argExpr->getSourceRange());
Steve Naroff17f76e02007-05-03 21:03:48 +0000642 break;
Steve Naroff1f4d7272007-05-11 04:00:31 +0000643 case CompatiblePointerDiscardsQualifiers:
Steve Naroffeb9da942007-05-30 00:06:37 +0000644 Diag(l, diag::ext_typecheck_passing_discards_qualifiers,
645 rhsType.getAsString(), lhsType.getAsString(),
Chris Lattner38dbdb22007-07-21 03:03:59 +0000646 Fn->getSourceRange(), argExpr->getSourceRange());
Steve Naroff1f4d7272007-05-11 04:00:31 +0000647 break;
Steve Naroff17f76e02007-05-03 21:03:48 +0000648 case Incompatible:
Steve Naroff8563f652007-05-28 19:25:56 +0000649 return Diag(l, diag::err_typecheck_passing_incompatible,
Chris Lattner2da14fb2007-12-20 00:26:33 +0000650 rhsType.getAsString(), lhsType.getAsString(),
651 Fn->getSourceRange(), argExpr->getSourceRange());
Steve Naroff17f76e02007-05-03 21:03:48 +0000652 }
653 }
Steve Naroff0b661582007-08-28 23:30:39 +0000654 if (NumArgsInCall > NumArgsInProto && proto->isVariadic()) {
655 // Promote the arguments (C99 6.5.2.2p7).
656 for (unsigned i = NumArgsInProto; i < NumArgsInCall; i++) {
657 Expr *argExpr = Args[i];
Steve Naroff83895f72007-09-16 03:34:24 +0000658 assert(argExpr && "ActOnCallExpr(): missing argument expression");
Steve Naroff0b661582007-08-28 23:30:39 +0000659
660 DefaultArgumentPromotion(argExpr);
661 if (Args[i] != argExpr) // The expression was converted.
662 Args[i] = argExpr; // Make sure we store the converted expression.
663 }
664 } else if (NumArgsInCall != NumArgsInProto && !proto->isVariadic()) {
665 // Even if the types checked, bail if the number of arguments don't match.
Steve Naroffb8c289d2007-05-08 22:18:00 +0000666 return true;
Steve Naroff0b661582007-08-28 23:30:39 +0000667 }
668 } else if (isa<FunctionTypeNoProto>(funcT)) {
669 // Promote the arguments (C99 6.5.2.2p6).
670 for (unsigned i = 0; i < NumArgsInCall; i++) {
671 Expr *argExpr = Args[i];
Steve Naroff83895f72007-09-16 03:34:24 +0000672 assert(argExpr && "ActOnCallExpr(): missing argument expression");
Steve Naroff0b661582007-08-28 23:30:39 +0000673
674 DefaultArgumentPromotion(argExpr);
675 if (Args[i] != argExpr) // The expression was converted.
676 Args[i] = argExpr; // Make sure we store the converted expression.
677 }
Steve Naroffae4143e2007-04-26 20:39:23 +0000678 }
Chris Lattnerb87b1b32007-08-10 20:18:51 +0000679 // Do special checking on direct calls to functions.
680 if (ImplicitCastExpr *IcExpr = dyn_cast<ImplicitCastExpr>(Fn))
681 if (DeclRefExpr *DRExpr = dyn_cast<DeclRefExpr>(IcExpr->getSubExpr()))
682 if (FunctionDecl *FDecl = dyn_cast<FunctionDecl>(DRExpr->getDecl()))
Chris Lattnere202e6a2007-12-20 00:05:45 +0000683 if (CheckFunctionCall(Fn, RParenLoc, FDecl, Args, NumArgsInCall)) {
684 // Function rejected, delete sub-ast's.
685 delete Fn;
686 for (unsigned i = 0; i != NumArgsInCall; ++i)
687 delete Args[i];
Anders Carlsson98f07902007-08-17 05:31:46 +0000688 return true;
Chris Lattnere202e6a2007-12-20 00:05:45 +0000689 }
Chris Lattnerb87b1b32007-08-10 20:18:51 +0000690
Chris Lattner38dbdb22007-07-21 03:03:59 +0000691 return new CallExpr(Fn, Args, NumArgsInCall, resultType, RParenLoc);
Chris Lattnere168f762006-11-10 05:29:30 +0000692}
693
694Action::ExprResult Sema::
Steve Naroff83895f72007-09-16 03:34:24 +0000695ActOnCompoundLiteral(SourceLocation LParenLoc, TypeTy *Ty,
Steve Naroff57eb2c52007-07-19 21:32:11 +0000696 SourceLocation RParenLoc, ExprTy *InitExpr) {
Steve Naroff83895f72007-09-16 03:34:24 +0000697 assert((Ty != 0) && "ActOnCompoundLiteral(): missing type");
Steve Narofffbd09832007-07-19 01:06:55 +0000698 QualType literalType = QualType::getFromOpaquePtr(Ty);
Steve Naroff57eb2c52007-07-19 21:32:11 +0000699 // FIXME: put back this assert when initializers are worked out.
Steve Naroff83895f72007-09-16 03:34:24 +0000700 //assert((InitExpr != 0) && "ActOnCompoundLiteral(): missing expression");
Steve Naroff57eb2c52007-07-19 21:32:11 +0000701 Expr *literalExpr = static_cast<Expr*>(InitExpr);
Anders Carlsson2c1ec6d2007-12-05 07:24:19 +0000702
Steve Naroff91f78082007-12-10 22:44:33 +0000703 // FIXME: add more semantic analysis (C99 6.5.2.5).
704 if (CheckInitializer(literalExpr, literalType, false))
705 return 0;
Anders Carlsson2c1ec6d2007-12-05 07:24:19 +0000706
Steve Naroff57eb2c52007-07-19 21:32:11 +0000707 return new CompoundLiteralExpr(literalType, literalExpr);
Steve Narofffbd09832007-07-19 01:06:55 +0000708}
709
710Action::ExprResult Sema::
Steve Naroff83895f72007-09-16 03:34:24 +0000711ActOnInitList(SourceLocation LBraceLoc, ExprTy **initlist, unsigned NumInit,
Anders Carlsson4692db02007-08-31 04:56:16 +0000712 SourceLocation RBraceLoc) {
Steve Naroff2fea1392007-09-02 02:04:30 +0000713 Expr **InitList = reinterpret_cast<Expr**>(initlist);
Anders Carlsson4692db02007-08-31 04:56:16 +0000714
Steve Naroff30d242c2007-09-15 18:49:24 +0000715 // Semantic analysis for initializers is done by ActOnDeclarator() and
Steve Naroff7d2c5ed2007-09-03 01:24:23 +0000716 // CheckInitializer() - it requires knowledge of the object being intialized.
Anders Carlsson4692db02007-08-31 04:56:16 +0000717
Steve Naroffb03f5942007-09-02 20:30:18 +0000718 InitListExpr *e = new InitListExpr(LBraceLoc, InitList, NumInit, RBraceLoc);
719 e->setType(Context.VoidTy); // FIXME: just a place holder for now.
720 return e;
Steve Narofffbd09832007-07-19 01:06:55 +0000721}
722
Chris Lattner6c9ffe92007-12-20 00:44:32 +0000723bool Sema::CheckVectorCast(SourceRange R, QualType VectorTy, QualType Ty) {
Anders Carlssonde71adf2007-11-27 05:51:55 +0000724 assert(VectorTy->isVectorType() && "Not a vector type!");
725
726 if (Ty->isVectorType() || Ty->isIntegerType()) {
727 if (Context.getTypeSize(VectorTy, SourceLocation()) !=
728 Context.getTypeSize(Ty, SourceLocation()))
729 return Diag(R.getBegin(),
730 Ty->isVectorType() ?
731 diag::err_invalid_conversion_between_vectors :
732 diag::err_invalid_conversion_between_vector_and_integer,
733 VectorTy.getAsString().c_str(),
734 Ty.getAsString().c_str(), R);
735 } else
736 return Diag(R.getBegin(),
737 diag::err_invalid_conversion_between_vector_and_scalar,
738 VectorTy.getAsString().c_str(),
739 Ty.getAsString().c_str(), R);
740
741 return false;
742}
743
Steve Narofffbd09832007-07-19 01:06:55 +0000744Action::ExprResult Sema::
Steve Naroff83895f72007-09-16 03:34:24 +0000745ActOnCastExpr(SourceLocation LParenLoc, TypeTy *Ty,
Chris Lattnere168f762006-11-10 05:29:30 +0000746 SourceLocation RParenLoc, ExprTy *Op) {
Steve Naroff83895f72007-09-16 03:34:24 +0000747 assert((Ty != 0) && (Op != 0) && "ActOnCastExpr(): missing type or expr");
Steve Naroff1a2cf6b2007-07-16 23:25:18 +0000748
749 Expr *castExpr = static_cast<Expr*>(Op);
750 QualType castType = QualType::getFromOpaquePtr(Ty);
751
Steve Naroff43b8f7f2007-08-31 00:32:44 +0000752 UsualUnaryConversions(castExpr);
753
Chris Lattnerbd270732007-07-18 16:00:06 +0000754 // C99 6.5.4p2: the cast type needs to be void or scalar and the expression
755 // type needs to be scalar.
Chris Lattner3bc4d202007-10-29 04:26:44 +0000756 if (!castType->isVoidType()) { // Cast to void allows any expr type.
757 if (!castType->isScalarType())
758 return Diag(LParenLoc, diag::err_typecheck_cond_expect_scalar,
759 castType.getAsString(), SourceRange(LParenLoc, RParenLoc));
Anders Carlssonde71adf2007-11-27 05:51:55 +0000760 if (!castExpr->getType()->isScalarType())
Chris Lattner3bc4d202007-10-29 04:26:44 +0000761 return Diag(castExpr->getLocStart(),
762 diag::err_typecheck_expect_scalar_operand,
763 castExpr->getType().getAsString(),castExpr->getSourceRange());
Anders Carlssonde71adf2007-11-27 05:51:55 +0000764
765 if (castExpr->getType()->isVectorType()) {
766 if (CheckVectorCast(SourceRange(LParenLoc, RParenLoc),
767 castExpr->getType(), castType))
768 return true;
769 } else if (castType->isVectorType()) {
770 if (CheckVectorCast(SourceRange(LParenLoc, RParenLoc),
771 castType, castExpr->getType()))
772 return true;
Chris Lattner3bc4d202007-10-29 04:26:44 +0000773 }
Steve Naroff1a2cf6b2007-07-16 23:25:18 +0000774 }
775 return new CastExpr(castType, castExpr, LParenLoc);
Chris Lattnere168f762006-11-10 05:29:30 +0000776}
777
Steve Naroffa78c4642007-10-18 05:13:08 +0000778// promoteExprToType - a helper function to ensure we create exactly one
779// ImplicitCastExpr.
780static void promoteExprToType(Expr *&expr, QualType type) {
781 if (ImplicitCastExpr *impCast = dyn_cast<ImplicitCastExpr>(expr))
782 impCast->setType(type);
783 else
784 expr = new ImplicitCastExpr(type, expr);
785 return;
786}
787
Chris Lattner2ab40a62007-11-26 01:40:58 +0000788/// Note that lex is not null here, even if this is the gnu "x ?: y" extension.
789/// In that case, lex = cond.
Steve Narofff8a28c52007-05-15 20:29:32 +0000790inline QualType Sema::CheckConditionalOperands( // C99 6.5.15
Steve Naroff7a5af782007-07-13 16:58:59 +0000791 Expr *&cond, Expr *&lex, Expr *&rex, SourceLocation questionLoc) {
Steve Naroff31090012007-07-16 21:54:35 +0000792 UsualUnaryConversions(cond);
793 UsualUnaryConversions(lex);
794 UsualUnaryConversions(rex);
795 QualType condT = cond->getType();
796 QualType lexT = lex->getType();
797 QualType rexT = rex->getType();
798
Steve Naroffa78fe7e2007-05-16 19:47:19 +0000799 // first, check the condition.
Steve Naroff7a5af782007-07-13 16:58:59 +0000800 if (!condT->isScalarType()) { // C99 6.5.15p2
801 Diag(cond->getLocStart(), diag::err_typecheck_cond_expect_scalar,
802 condT.getAsString());
Steve Naroffa78fe7e2007-05-16 19:47:19 +0000803 return QualType();
804 }
805 // now check the two expressions.
Steve Naroffdbd9e892007-07-17 00:58:39 +0000806 if (lexT->isArithmeticType() && rexT->isArithmeticType()) { // C99 6.5.15p3,5
807 UsualArithmeticConversions(lex, rex);
808 return lex->getType();
809 }
Chris Lattnere5a6cbd2007-07-31 21:27:01 +0000810 if (const RecordType *LHSRT = lexT->getAsRecordType()) { // C99 6.5.15p3
811 if (const RecordType *RHSRT = rexT->getAsRecordType()) {
Chris Lattner2ab40a62007-11-26 01:40:58 +0000812 if (LHSRT->getDecl() == RHSRT->getDecl())
Chris Lattnere5a6cbd2007-07-31 21:27:01 +0000813 return lexT;
814
Steve Naroffa78fe7e2007-05-16 19:47:19 +0000815 Diag(questionLoc, diag::err_typecheck_cond_incompatible_operands,
Steve Naroff7a5af782007-07-13 16:58:59 +0000816 lexT.getAsString(), rexT.getAsString(),
817 lex->getSourceRange(), rex->getSourceRange());
Steve Naroffa78fe7e2007-05-16 19:47:19 +0000818 return QualType();
819 }
820 }
Chris Lattner0e9d6222007-07-15 23:26:56 +0000821 // C99 6.5.15p3
Steve Naroffa78c4642007-10-18 05:13:08 +0000822 if (lexT->isPointerType() && rex->isNullPointerConstant(Context)) {
823 promoteExprToType(rex, lexT); // promote the null to a pointer.
Steve Naroff7a5af782007-07-13 16:58:59 +0000824 return lexT;
Steve Naroffa78c4642007-10-18 05:13:08 +0000825 }
826 if (rexT->isPointerType() && lex->isNullPointerConstant(Context)) {
827 promoteExprToType(lex, rexT); // promote the null to a pointer.
Steve Naroff7a5af782007-07-13 16:58:59 +0000828 return rexT;
Steve Naroffa78c4642007-10-18 05:13:08 +0000829 }
Chris Lattnere5a6cbd2007-07-31 21:27:01 +0000830 if (const PointerType *LHSPT = lexT->getAsPointerType()) { // C99 6.5.15p3,6
831 if (const PointerType *RHSPT = rexT->getAsPointerType()) {
832 // get the "pointed to" types
833 QualType lhptee = LHSPT->getPointeeType();
834 QualType rhptee = RHSPT->getPointeeType();
Steve Naroffa78fe7e2007-05-16 19:47:19 +0000835
Chris Lattnere5a6cbd2007-07-31 21:27:01 +0000836 // ignore qualifiers on void (C99 6.5.15p3, clause 6)
837 if (lhptee->isVoidType() &&
838 (rhptee->isObjectType() || rhptee->isIncompleteType()))
839 return lexT;
840 if (rhptee->isVoidType() &&
841 (lhptee->isObjectType() || lhptee->isIncompleteType()))
842 return rexT;
Steve Naroffa78fe7e2007-05-16 19:47:19 +0000843
Steve Naroff32e44c02007-10-15 20:41:53 +0000844 if (!Context.typesAreCompatible(lhptee.getUnqualifiedType(),
845 rhptee.getUnqualifiedType())) {
Chris Lattnere5a6cbd2007-07-31 21:27:01 +0000846 Diag(questionLoc, diag::ext_typecheck_cond_incompatible_pointers,
847 lexT.getAsString(), rexT.getAsString(),
848 lex->getSourceRange(), rex->getSourceRange());
849 return lexT; // FIXME: this is an _ext - is this return o.k?
850 }
851 // The pointer types are compatible.
Chris Lattnerf17bd422007-08-30 17:45:32 +0000852 // C99 6.5.15p6: If both operands are pointers to compatible types *or* to
853 // differently qualified versions of compatible types, the result type is
854 // a pointer to an appropriately qualified version of the *composite*
855 // type.
Chris Lattnere5a6cbd2007-07-31 21:27:01 +0000856 return lexT; // FIXME: Need to return the composite type.
Steve Naroffa78fe7e2007-05-16 19:47:19 +0000857 }
858 }
Chris Lattnere5a6cbd2007-07-31 21:27:01 +0000859
Steve Naroff7a5af782007-07-13 16:58:59 +0000860 if (lexT->isVoidType() && rexT->isVoidType()) // C99 6.5.15p3
861 return lexT;
Steve Naroffa78fe7e2007-05-16 19:47:19 +0000862
863 Diag(questionLoc, diag::err_typecheck_cond_incompatible_operands,
Steve Naroff7a5af782007-07-13 16:58:59 +0000864 lexT.getAsString(), rexT.getAsString(),
865 lex->getSourceRange(), rex->getSourceRange());
Steve Naroffa78fe7e2007-05-16 19:47:19 +0000866 return QualType();
Steve Narofff8a28c52007-05-15 20:29:32 +0000867}
868
Steve Naroff83895f72007-09-16 03:34:24 +0000869/// ActOnConditionalOp - Parse a ?: operation. Note that 'LHS' may be null
Chris Lattnere168f762006-11-10 05:29:30 +0000870/// in the case of a the GNU conditional expr extension.
Steve Naroff83895f72007-09-16 03:34:24 +0000871Action::ExprResult Sema::ActOnConditionalOp(SourceLocation QuestionLoc,
Chris Lattnere168f762006-11-10 05:29:30 +0000872 SourceLocation ColonLoc,
873 ExprTy *Cond, ExprTy *LHS,
874 ExprTy *RHS) {
Chris Lattnerdaaa9f22007-07-16 21:39:03 +0000875 Expr *CondExpr = (Expr *) Cond;
876 Expr *LHSExpr = (Expr *) LHS, *RHSExpr = (Expr *) RHS;
Chris Lattner2ab40a62007-11-26 01:40:58 +0000877
878 // If this is the gnu "x ?: y" extension, analyze the types as though the LHS
879 // was the condition.
880 bool isLHSNull = LHSExpr == 0;
881 if (isLHSNull)
882 LHSExpr = CondExpr;
883
Chris Lattnerdaaa9f22007-07-16 21:39:03 +0000884 QualType result = CheckConditionalOperands(CondExpr, LHSExpr,
885 RHSExpr, QuestionLoc);
Steve Narofff8a28c52007-05-15 20:29:32 +0000886 if (result.isNull())
887 return true;
Chris Lattner2ab40a62007-11-26 01:40:58 +0000888 return new ConditionalOperator(CondExpr, isLHSNull ? 0 : LHSExpr,
889 RHSExpr, result);
Chris Lattnere168f762006-11-10 05:29:30 +0000890}
891
Steve Naroff0b661582007-08-28 23:30:39 +0000892/// DefaultArgumentPromotion (C99 6.5.2.2p6). Used for function calls that
893/// do not have a prototype. Integer promotions are performed on each
894/// argument, and arguments that have type float are promoted to double.
895void Sema::DefaultArgumentPromotion(Expr *&expr) {
896 QualType t = expr->getType();
897 assert(!t.isNull() && "DefaultArgumentPromotion - missing type");
898
899 if (t->isPromotableIntegerType()) // C99 6.3.1.1p2
900 promoteExprToType(expr, Context.IntTy);
901 if (t == Context.FloatTy)
902 promoteExprToType(expr, Context.DoubleTy);
903}
904
Steve Naroff81569d22007-07-15 02:02:06 +0000905/// DefaultFunctionArrayConversion (C99 6.3.2.1p3, C99 6.3.2.1p4).
Steve Naroff31090012007-07-16 21:54:35 +0000906void Sema::DefaultFunctionArrayConversion(Expr *&e) {
Steve Naroff81569d22007-07-15 02:02:06 +0000907 QualType t = e->getType();
Steve Naroffb8ea4fb2007-07-13 23:32:42 +0000908 assert(!t.isNull() && "DefaultFunctionArrayConversion - missing type");
Bill Wendlingdfc81072007-07-17 03:52:31 +0000909
Chris Lattnercd1d0862007-07-31 16:56:34 +0000910 if (const ReferenceType *ref = t->getAsReferenceType()) {
Bill Wendling354fb262007-07-17 04:16:47 +0000911 promoteExprToType(e, ref->getReferenceeType()); // C++ [expr]
912 t = e->getType();
913 }
Steve Naroff81569d22007-07-15 02:02:06 +0000914 if (t->isFunctionType())
Steve Naroff31090012007-07-16 21:54:35 +0000915 promoteExprToType(e, Context.getPointerType(t));
Chris Lattner41977962007-07-31 19:29:30 +0000916 else if (const ArrayType *ary = t->getAsArrayType())
Steve Naroff31090012007-07-16 21:54:35 +0000917 promoteExprToType(e, Context.getPointerType(ary->getElementType()));
Steve Naroff1926c832007-04-24 00:23:05 +0000918}
919
Steve Naroff71b59a92007-06-04 22:22:31 +0000920/// UsualUnaryConversion - Performs various conversions that are common to most
921/// operators (C99 6.3). The conversions of array and function types are
922/// sometimes surpressed. For example, the array->pointer conversion doesn't
923/// apply if the array is an argument to the sizeof or address (&) operators.
924/// In these instances, this routine should *not* be called.
Steve Naroff31090012007-07-16 21:54:35 +0000925void Sema::UsualUnaryConversions(Expr *&expr) {
Steve Naroff7a5af782007-07-13 16:58:59 +0000926 QualType t = expr->getType();
Steve Naroff71b59a92007-06-04 22:22:31 +0000927 assert(!t.isNull() && "UsualUnaryConversions - missing type");
928
Chris Lattnercd1d0862007-07-31 16:56:34 +0000929 if (const ReferenceType *ref = t->getAsReferenceType()) {
Bill Wendling354fb262007-07-17 04:16:47 +0000930 promoteExprToType(expr, ref->getReferenceeType()); // C++ [expr]
931 t = expr->getType();
932 }
Steve Naroff81569d22007-07-15 02:02:06 +0000933 if (t->isPromotableIntegerType()) // C99 6.3.1.1p2
Steve Naroff31090012007-07-16 21:54:35 +0000934 promoteExprToType(expr, Context.IntTy);
935 else
936 DefaultFunctionArrayConversion(expr);
Steve Naroff71b59a92007-06-04 22:22:31 +0000937}
938
Chris Lattnerf17bd422007-08-30 17:45:32 +0000939/// UsualArithmeticConversions - Performs various conversions that are common to
Steve Naroff1926c832007-04-24 00:23:05 +0000940/// binary operators (C99 6.3.1.8). If both operands aren't arithmetic, this
941/// routine returns the first non-arithmetic type found. The client is
942/// responsible for emitting appropriate error diagnostics.
Steve Naroffbe4c4d12007-08-24 19:07:16 +0000943QualType Sema::UsualArithmeticConversions(Expr *&lhsExpr, Expr *&rhsExpr,
944 bool isCompAssign) {
Steve Naroff46c72912007-08-25 19:54:59 +0000945 if (!isCompAssign) {
946 UsualUnaryConversions(lhsExpr);
947 UsualUnaryConversions(rhsExpr);
948 }
Steve Naroff1bb21df2007-10-18 18:55:53 +0000949 // For conversion purposes, we ignore any qualifiers.
950 // For example, "const float" and "float" are equivalent.
Steve Naroff257b4a22007-11-10 19:45:54 +0000951 QualType lhs = lhsExpr->getType().getCanonicalType().getUnqualifiedType();
952 QualType rhs = rhsExpr->getType().getCanonicalType().getUnqualifiedType();
Steve Naroff1926c832007-04-24 00:23:05 +0000953
Chris Lattner0c46c5d2007-06-02 23:53:17 +0000954 // If both types are identical, no conversion is needed.
Steve Naroff1bb21df2007-10-18 18:55:53 +0000955 if (lhs == rhs)
956 return lhs;
Chris Lattner0c46c5d2007-06-02 23:53:17 +0000957
958 // If either side is a non-arithmetic type (e.g. a pointer), we are done.
959 // The caller can deal with this (e.g. pointer + int).
Steve Naroffdbd9e892007-07-17 00:58:39 +0000960 if (!lhs->isArithmeticType() || !rhs->isArithmeticType())
Steve Naroffbe4c4d12007-08-24 19:07:16 +0000961 return lhs;
Steve Naroff1926c832007-04-24 00:23:05 +0000962
Chris Lattner0c46c5d2007-06-02 23:53:17 +0000963 // At this point, we have two different arithmetic types.
Steve Naroffb01bbe32007-04-25 01:22:31 +0000964
965 // Handle complex types first (C99 6.3.1.8p1).
966 if (lhs->isComplexType() || rhs->isComplexType()) {
967 // if we have an integer operand, the result is the complex type.
Steve Naroffdbd9e892007-07-17 00:58:39 +0000968 if (rhs->isIntegerType()) { // convert the rhs to the lhs complex type.
Steve Naroffbe4c4d12007-08-24 19:07:16 +0000969 if (!isCompAssign) promoteExprToType(rhsExpr, lhs);
970 return lhs;
Steve Naroffdbd9e892007-07-17 00:58:39 +0000971 }
972 if (lhs->isIntegerType()) { // convert the lhs to the rhs complex type.
Steve Naroffbe4c4d12007-08-24 19:07:16 +0000973 if (!isCompAssign) promoteExprToType(lhsExpr, rhs);
974 return rhs;
Steve Naroffdbd9e892007-07-17 00:58:39 +0000975 }
Steve Naroff9091ef72007-08-27 01:27:54 +0000976 // This handles complex/complex, complex/float, or float/complex.
977 // When both operands are complex, the shorter operand is converted to the
978 // type of the longer, and that is the type of the result. This corresponds
979 // to what is done when combining two real floating-point operands.
980 // The fun begins when size promotion occur across type domains.
981 // From H&S 6.3.4: When one operand is complex and the other is a real
982 // floating-point type, the less precise type is converted, within it's
983 // real or complex domain, to the precision of the other type. For example,
984 // when combining a "long double" with a "double _Complex", the
985 // "double _Complex" is promoted to "long double _Complex".
Steve Naroff7af82d42007-08-27 15:30:22 +0000986 int result = Context.compareFloatingType(lhs, rhs);
987
988 if (result > 0) { // The left side is bigger, convert rhs.
Steve Naroffe31313d2007-08-27 21:32:55 +0000989 rhs = Context.getFloatingTypeOfSizeWithinDomain(lhs, rhs);
990 if (!isCompAssign)
991 promoteExprToType(rhsExpr, rhs);
992 } else if (result < 0) { // The right side is bigger, convert lhs.
993 lhs = Context.getFloatingTypeOfSizeWithinDomain(rhs, lhs);
994 if (!isCompAssign)
995 promoteExprToType(lhsExpr, lhs);
996 }
997 // At this point, lhs and rhs have the same rank/size. Now, make sure the
998 // domains match. This is a requirement for our implementation, C99
999 // does not require this promotion.
1000 if (lhs != rhs) { // Domains don't match, we have complex/float mix.
1001 if (lhs->isRealFloatingType()) { // handle "double, _Complex double".
Steve Naroffa042db22007-08-27 21:43:43 +00001002 if (!isCompAssign)
1003 promoteExprToType(lhsExpr, rhs);
1004 return rhs;
Steve Naroffe31313d2007-08-27 21:32:55 +00001005 } else { // handle "_Complex double, double".
Steve Naroffa042db22007-08-27 21:43:43 +00001006 if (!isCompAssign)
1007 promoteExprToType(rhsExpr, lhs);
1008 return lhs;
Steve Naroffe31313d2007-08-27 21:32:55 +00001009 }
Steve Naroffdbd9e892007-07-17 00:58:39 +00001010 }
Steve Naroffa042db22007-08-27 21:43:43 +00001011 return lhs; // The domain/size match exactly.
Steve Naroffbf223ba2007-04-24 20:56:26 +00001012 }
Steve Naroffb01bbe32007-04-25 01:22:31 +00001013 // Now handle "real" floating types (i.e. float, double, long double).
1014 if (lhs->isRealFloatingType() || rhs->isRealFloatingType()) {
1015 // if we have an integer operand, the result is the real floating type.
Steve Naroffdbd9e892007-07-17 00:58:39 +00001016 if (rhs->isIntegerType()) { // convert rhs to the lhs floating point type.
Steve Naroffbe4c4d12007-08-24 19:07:16 +00001017 if (!isCompAssign) promoteExprToType(rhsExpr, lhs);
1018 return lhs;
Steve Naroffdbd9e892007-07-17 00:58:39 +00001019 }
1020 if (lhs->isIntegerType()) { // convert lhs to the rhs floating point type.
Steve Naroffbe4c4d12007-08-24 19:07:16 +00001021 if (!isCompAssign) promoteExprToType(lhsExpr, rhs);
1022 return rhs;
Steve Naroffdbd9e892007-07-17 00:58:39 +00001023 }
Steve Naroff81569d22007-07-15 02:02:06 +00001024 // We have two real floating types, float/complex combos were handled above.
1025 // Convert the smaller operand to the bigger result.
Steve Naroff7af82d42007-08-27 15:30:22 +00001026 int result = Context.compareFloatingType(lhs, rhs);
1027
1028 if (result > 0) { // convert the rhs
Steve Naroffbe4c4d12007-08-24 19:07:16 +00001029 if (!isCompAssign) promoteExprToType(rhsExpr, lhs);
1030 return lhs;
Steve Naroffdbd9e892007-07-17 00:58:39 +00001031 }
Steve Naroff7af82d42007-08-27 15:30:22 +00001032 if (result < 0) { // convert the lhs
1033 if (!isCompAssign) promoteExprToType(lhsExpr, rhs); // convert the lhs
1034 return rhs;
1035 }
1036 assert(0 && "Sema::UsualArithmeticConversions(): illegal float comparison");
Steve Naroffb01bbe32007-04-25 01:22:31 +00001037 }
Steve Naroff81569d22007-07-15 02:02:06 +00001038 // Finally, we have two differing integer types.
Steve Naroffdbd9e892007-07-17 00:58:39 +00001039 if (Context.maxIntegerType(lhs, rhs) == lhs) { // convert the rhs
Steve Naroffbe4c4d12007-08-24 19:07:16 +00001040 if (!isCompAssign) promoteExprToType(rhsExpr, lhs);
1041 return lhs;
Steve Naroffdbd9e892007-07-17 00:58:39 +00001042 }
Steve Naroffbe4c4d12007-08-24 19:07:16 +00001043 if (!isCompAssign) promoteExprToType(lhsExpr, rhs); // convert the lhs
1044 return rhs;
Steve Narofff1e53692007-03-23 22:27:02 +00001045}
1046
Steve Naroff3f597292007-05-11 22:18:03 +00001047// CheckPointerTypesForAssignment - This is a very tricky routine (despite
1048// being closely modeled after the C99 spec:-). The odd characteristic of this
1049// routine is it effectively iqnores the qualifiers on the top level pointee.
1050// This circumvents the usual type rules specified in 6.2.7p1 & 6.7.5.[1-3].
1051// FIXME: add a couple examples in this comment.
Steve Naroff98cf3e92007-06-06 18:38:38 +00001052Sema::AssignmentCheckResult
1053Sema::CheckPointerTypesForAssignment(QualType lhsType, QualType rhsType) {
Steve Naroff3f597292007-05-11 22:18:03 +00001054 QualType lhptee, rhptee;
Steve Naroff1f4d7272007-05-11 04:00:31 +00001055
1056 // get the "pointed to" type (ignoring qualifiers at the top level)
Chris Lattnere5a6cbd2007-07-31 21:27:01 +00001057 lhptee = lhsType->getAsPointerType()->getPointeeType();
1058 rhptee = rhsType->getAsPointerType()->getPointeeType();
Steve Naroff1f4d7272007-05-11 04:00:31 +00001059
1060 // make sure we operate on the canonical type
Steve Naroff3f597292007-05-11 22:18:03 +00001061 lhptee = lhptee.getCanonicalType();
1062 rhptee = rhptee.getCanonicalType();
Steve Naroff1f4d7272007-05-11 04:00:31 +00001063
Steve Naroff98cf3e92007-06-06 18:38:38 +00001064 AssignmentCheckResult r = Compatible;
1065
Steve Naroff3f597292007-05-11 22:18:03 +00001066 // C99 6.5.16.1p1: This following citation is common to constraints
1067 // 3 & 4 (below). ...and the type *pointed to* by the left has all the
1068 // qualifiers of the type *pointed to* by the right;
1069 if ((lhptee.getQualifiers() & rhptee.getQualifiers()) !=
1070 rhptee.getQualifiers())
1071 r = CompatiblePointerDiscardsQualifiers;
1072
1073 // C99 6.5.16.1p1 (constraint 4): If one operand is a pointer to an object or
1074 // incomplete type and the other is a pointer to a qualified or unqualified
1075 // version of void...
1076 if (lhptee.getUnqualifiedType()->isVoidType() &&
1077 (rhptee->isObjectType() || rhptee->isIncompleteType()))
1078 ;
1079 else if (rhptee.getUnqualifiedType()->isVoidType() &&
1080 (lhptee->isObjectType() || lhptee->isIncompleteType()))
1081 ;
1082 // C99 6.5.16.1p1 (constraint 3): both operands are pointers to qualified or
1083 // unqualified versions of compatible types, ...
Steve Naroff32e44c02007-10-15 20:41:53 +00001084 else if (!Context.typesAreCompatible(lhptee.getUnqualifiedType(),
1085 rhptee.getUnqualifiedType()))
Steve Naroff3f597292007-05-11 22:18:03 +00001086 r = IncompatiblePointer; // this "trumps" PointerAssignDiscardsQualifiers
Steve Naroff98cf3e92007-06-06 18:38:38 +00001087 return r;
Steve Naroff1f4d7272007-05-11 04:00:31 +00001088}
1089
Steve Naroff98cf3e92007-06-06 18:38:38 +00001090/// CheckAssignmentConstraints (C99 6.5.16) - This routine currently
Steve Naroff17f76e02007-05-03 21:03:48 +00001091/// has code to accommodate several GCC extensions when type checking
1092/// pointers. Here are some objectionable examples that GCC considers warnings:
1093///
1094/// int a, *pint;
1095/// short *pshort;
1096/// struct foo *pfoo;
1097///
1098/// pint = pshort; // warning: assignment from incompatible pointer type
1099/// a = pint; // warning: assignment makes integer from pointer without a cast
1100/// pint = a; // warning: assignment makes pointer from integer without a cast
1101/// pint = pfoo; // warning: assignment from incompatible pointer type
1102///
1103/// As a result, the code for dealing with pointers is more complex than the
1104/// C99 spec dictates.
1105/// Note: the warning above turn into errors when -pedantic-errors is enabled.
1106///
Steve Naroff98cf3e92007-06-06 18:38:38 +00001107Sema::AssignmentCheckResult
1108Sema::CheckAssignmentConstraints(QualType lhsType, QualType rhsType) {
Fariborz Jahaniana1e34202007-12-19 17:45:58 +00001109
1110
Steve Naroff4a158f32007-11-13 00:31:42 +00001111 if (lhsType.getCanonicalType().getUnqualifiedType() ==
1112 rhsType.getCanonicalType().getUnqualifiedType())
Chris Lattnerb104d312007-10-29 05:15:40 +00001113 return Compatible; // common case, fast path...
Steve Naroff44fd8ff2007-07-24 21:46:40 +00001114
Anders Carlsson24ebce62007-10-12 23:56:29 +00001115 if (lhsType->isReferenceType() || rhsType->isReferenceType()) {
Steve Naroff32e44c02007-10-15 20:41:53 +00001116 if (Context.referenceTypesAreCompatible(lhsType, rhsType))
Anders Carlsson24ebce62007-10-12 23:56:29 +00001117 return Compatible;
Fariborz Jahaniana1e34202007-12-19 17:45:58 +00001118 }
1119 else if (lhsType->isObjcQualifiedIdType()
1120 || rhsType->isObjcQualifiedIdType()) {
1121 if (Context.ObjcQualifiedIdTypesAreCompatible(lhsType, rhsType))
1122 return Compatible;
1123 }
1124 else if (lhsType->isArithmeticType() && rhsType->isArithmeticType()) {
Steve Naroffe728ba32007-07-10 22:20:04 +00001125 if (lhsType->isVectorType() || rhsType->isVectorType()) {
Anders Carlssone1af1d22007-11-30 04:21:22 +00001126 if (!getLangOptions().LaxVectorConversions) {
1127 if (lhsType.getCanonicalType() != rhsType.getCanonicalType())
1128 return Incompatible;
1129 } else {
1130 if (lhsType->isVectorType() && rhsType->isVectorType()) {
1131 if ((lhsType->isIntegerType() && rhsType->isIntegerType()) ||
1132 (lhsType->isRealFloatingType() &&
1133 rhsType->isRealFloatingType())) {
1134 if (Context.getTypeSize(lhsType, SourceLocation()) ==
1135 Context.getTypeSize(rhsType, SourceLocation()))
1136 return Compatible;
1137 }
1138 }
Steve Naroffe728ba32007-07-10 22:20:04 +00001139 return Incompatible;
Anders Carlssone1af1d22007-11-30 04:21:22 +00001140 }
1141 }
Steve Naroff98cf3e92007-06-06 18:38:38 +00001142 return Compatible;
Steve Naroff84ff4b42007-07-09 21:31:10 +00001143 } else if (lhsType->isPointerType()) {
Steve Naroff98cf3e92007-06-06 18:38:38 +00001144 if (rhsType->isIntegerType())
1145 return PointerFromInt;
1146
Steve Naroff3f597292007-05-11 22:18:03 +00001147 if (rhsType->isPointerType())
Steve Naroff98cf3e92007-06-06 18:38:38 +00001148 return CheckPointerTypesForAssignment(lhsType, rhsType);
Steve Naroff9eb24652007-05-02 21:58:15 +00001149 } else if (rhsType->isPointerType()) {
Steve Naroff98cf3e92007-06-06 18:38:38 +00001150 // C99 6.5.16.1p1: the left operand is _Bool and the right is a pointer.
1151 if ((lhsType->isIntegerType()) && (lhsType != Context.BoolTy))
1152 return IntFromPointer;
1153
Steve Naroff3f597292007-05-11 22:18:03 +00001154 if (lhsType->isPointerType())
Steve Naroff98cf3e92007-06-06 18:38:38 +00001155 return CheckPointerTypesForAssignment(lhsType, rhsType);
Steve Naroff1f4d7272007-05-11 04:00:31 +00001156 } else if (isa<TagType>(lhsType) && isa<TagType>(rhsType)) {
Steve Naroff32e44c02007-10-15 20:41:53 +00001157 if (Context.tagTypesAreCompatible(lhsType, rhsType))
Steve Naroff98cf3e92007-06-06 18:38:38 +00001158 return Compatible;
Bill Wendling216423b2007-05-30 06:30:29 +00001159 }
Steve Naroff98cf3e92007-06-06 18:38:38 +00001160 return Incompatible;
Steve Naroff9eb24652007-05-02 21:58:15 +00001161}
1162
Steve Naroffb8ea4fb2007-07-13 23:32:42 +00001163Sema::AssignmentCheckResult
1164Sema::CheckSingleAssignmentConstraints(QualType lhsType, Expr *&rExpr) {
Steve Naroff0ee0b0a2007-11-27 17:58:44 +00001165 // C99 6.5.16.1p1: the left operand is a pointer and the right is
1166 // a null pointer constant.
1167 if (lhsType->isPointerType() && rExpr->isNullPointerConstant(Context)) {
1168 promoteExprToType(rExpr, lhsType);
1169 return Compatible;
1170 }
Chris Lattnere6dcd502007-10-16 02:55:40 +00001171 // This check seems unnatural, however it is necessary to ensure the proper
Steve Naroffb8ea4fb2007-07-13 23:32:42 +00001172 // conversion of functions/arrays. If the conversion were done for all
Steve Naroff30d242c2007-09-15 18:49:24 +00001173 // DeclExpr's (created by ActOnIdentifierExpr), it would mess up the unary
Steve Naroffb8ea4fb2007-07-13 23:32:42 +00001174 // expressions that surpress this implicit conversion (&, sizeof).
Chris Lattnere6dcd502007-10-16 02:55:40 +00001175 //
1176 // Suppress this for references: C99 8.5.3p5. FIXME: revisit when references
1177 // are better understood.
1178 if (!lhsType->isReferenceType())
1179 DefaultFunctionArrayConversion(rExpr);
Steve Naroff0c1c7ed2007-08-24 22:33:52 +00001180
1181 Sema::AssignmentCheckResult result;
Steve Naroffb8ea4fb2007-07-13 23:32:42 +00001182
Steve Naroff0c1c7ed2007-08-24 22:33:52 +00001183 result = CheckAssignmentConstraints(lhsType, rExpr->getType());
1184
1185 // C99 6.5.16.1p2: The value of the right operand is converted to the
1186 // type of the assignment expression.
1187 if (rExpr->getType() != lhsType)
1188 promoteExprToType(rExpr, lhsType);
1189 return result;
Steve Naroffb8ea4fb2007-07-13 23:32:42 +00001190}
1191
1192Sema::AssignmentCheckResult
1193Sema::CheckCompoundAssignmentConstraints(QualType lhsType, QualType rhsType) {
1194 return CheckAssignmentConstraints(lhsType, rhsType);
1195}
1196
Chris Lattner5c11c412007-12-12 05:47:28 +00001197QualType Sema::InvalidOperands(SourceLocation loc, Expr *&lex, Expr *&rex) {
Steve Naroff6f49f5d2007-05-29 14:23:36 +00001198 Diag(loc, diag::err_typecheck_invalid_operands,
1199 lex->getType().getAsString(), rex->getType().getAsString(),
1200 lex->getSourceRange(), rex->getSourceRange());
Chris Lattner5c11c412007-12-12 05:47:28 +00001201 return QualType();
Steve Naroff6f49f5d2007-05-29 14:23:36 +00001202}
1203
Steve Naroff7a5af782007-07-13 16:58:59 +00001204inline QualType Sema::CheckVectorOperands(SourceLocation loc, Expr *&lex,
1205 Expr *&rex) {
Steve Naroff84ff4b42007-07-09 21:31:10 +00001206 QualType lhsType = lex->getType(), rhsType = rex->getType();
1207
1208 // make sure the vector types are identical.
1209 if (lhsType == rhsType)
1210 return lhsType;
1211 // You cannot convert between vector values of different size.
1212 Diag(loc, diag::err_typecheck_vector_not_convertable,
1213 lex->getType().getAsString(), rex->getType().getAsString(),
1214 lex->getSourceRange(), rex->getSourceRange());
1215 return QualType();
1216}
1217
Steve Naroff218bc2b2007-05-04 21:54:46 +00001218inline QualType Sema::CheckMultiplyDivideOperands(
Steve Naroffbe4c4d12007-08-24 19:07:16 +00001219 Expr *&lex, Expr *&rex, SourceLocation loc, bool isCompAssign)
Steve Naroff5c10d4b2007-04-20 23:42:24 +00001220{
Steve Naroffb8ea4fb2007-07-13 23:32:42 +00001221 QualType lhsType = lex->getType(), rhsType = rex->getType();
1222
1223 if (lhsType->isVectorType() || rhsType->isVectorType())
Steve Naroff84ff4b42007-07-09 21:31:10 +00001224 return CheckVectorOperands(loc, lex, rex);
Steve Naroff7a5af782007-07-13 16:58:59 +00001225
Steve Naroffbe4c4d12007-08-24 19:07:16 +00001226 QualType compType = UsualArithmeticConversions(lex, rex, isCompAssign);
Steve Naroff5c10d4b2007-04-20 23:42:24 +00001227
Steve Naroffdbd9e892007-07-17 00:58:39 +00001228 if (lex->getType()->isArithmeticType() && rex->getType()->isArithmeticType())
Steve Naroffbe4c4d12007-08-24 19:07:16 +00001229 return compType;
Chris Lattner5c11c412007-12-12 05:47:28 +00001230 return InvalidOperands(loc, lex, rex);
Steve Naroff26c8ea52007-03-21 21:08:52 +00001231}
1232
Steve Naroff218bc2b2007-05-04 21:54:46 +00001233inline QualType Sema::CheckRemainderOperands(
Steve Naroffbe4c4d12007-08-24 19:07:16 +00001234 Expr *&lex, Expr *&rex, SourceLocation loc, bool isCompAssign)
Steve Naroff218bc2b2007-05-04 21:54:46 +00001235{
Steve Naroffb8ea4fb2007-07-13 23:32:42 +00001236 QualType lhsType = lex->getType(), rhsType = rex->getType();
1237
Steve Naroffbe4c4d12007-08-24 19:07:16 +00001238 QualType compType = UsualArithmeticConversions(lex, rex, isCompAssign);
Steve Naroff218bc2b2007-05-04 21:54:46 +00001239
Steve Naroffdbd9e892007-07-17 00:58:39 +00001240 if (lex->getType()->isIntegerType() && rex->getType()->isIntegerType())
Steve Naroffbe4c4d12007-08-24 19:07:16 +00001241 return compType;
Chris Lattner5c11c412007-12-12 05:47:28 +00001242 return InvalidOperands(loc, lex, rex);
Steve Naroff218bc2b2007-05-04 21:54:46 +00001243}
1244
1245inline QualType Sema::CheckAdditionOperands( // C99 6.5.6
Steve Naroffbe4c4d12007-08-24 19:07:16 +00001246 Expr *&lex, Expr *&rex, SourceLocation loc, bool isCompAssign)
Steve Naroff1926c832007-04-24 00:23:05 +00001247{
Steve Naroff94a5aca2007-07-16 22:23:01 +00001248 if (lex->getType()->isVectorType() || rex->getType()->isVectorType())
Steve Naroff7a5af782007-07-13 16:58:59 +00001249 return CheckVectorOperands(loc, lex, rex);
1250
Steve Naroffbe4c4d12007-08-24 19:07:16 +00001251 QualType compType = UsualArithmeticConversions(lex, rex, isCompAssign);
Steve Naroff94a5aca2007-07-16 22:23:01 +00001252
Steve Naroffe4718892007-04-27 18:30:00 +00001253 // handle the common case first (both operands are arithmetic).
Steve Naroffdbd9e892007-07-17 00:58:39 +00001254 if (lex->getType()->isArithmeticType() && rex->getType()->isArithmeticType())
Steve Naroffbe4c4d12007-08-24 19:07:16 +00001255 return compType;
Steve Naroff218bc2b2007-05-04 21:54:46 +00001256
Steve Naroffdbd9e892007-07-17 00:58:39 +00001257 if (lex->getType()->isPointerType() && rex->getType()->isIntegerType())
1258 return lex->getType();
1259 if (lex->getType()->isIntegerType() && rex->getType()->isPointerType())
1260 return rex->getType();
Chris Lattner5c11c412007-12-12 05:47:28 +00001261 return InvalidOperands(loc, lex, rex);
Steve Naroff26c8ea52007-03-21 21:08:52 +00001262}
1263
Steve Naroff218bc2b2007-05-04 21:54:46 +00001264inline QualType Sema::CheckSubtractionOperands( // C99 6.5.6
Steve Naroffbe4c4d12007-08-24 19:07:16 +00001265 Expr *&lex, Expr *&rex, SourceLocation loc, bool isCompAssign)
Steve Naroff218bc2b2007-05-04 21:54:46 +00001266{
Steve Naroff94a5aca2007-07-16 22:23:01 +00001267 if (lex->getType()->isVectorType() || rex->getType()->isVectorType())
Steve Naroff84ff4b42007-07-09 21:31:10 +00001268 return CheckVectorOperands(loc, lex, rex);
Steve Naroffb8ea4fb2007-07-13 23:32:42 +00001269
Steve Naroffbe4c4d12007-08-24 19:07:16 +00001270 QualType compType = UsualArithmeticConversions(lex, rex, isCompAssign);
Steve Naroff218bc2b2007-05-04 21:54:46 +00001271
Chris Lattner4d62f422007-12-09 21:53:25 +00001272 // Enforce type constraints: C99 6.5.6p3.
1273
1274 // Handle the common case first (both operands are arithmetic).
Steve Naroffdbd9e892007-07-17 00:58:39 +00001275 if (lex->getType()->isArithmeticType() && rex->getType()->isArithmeticType())
Steve Naroffbe4c4d12007-08-24 19:07:16 +00001276 return compType;
Chris Lattner4d62f422007-12-09 21:53:25 +00001277
1278 // Either ptr - int or ptr - ptr.
1279 if (const PointerType *LHSPTy = lex->getType()->getAsPointerType()) {
1280 // The LHS must be an object type, not incomplete, function, etc.
1281 if (!LHSPTy->getPointeeType()->isObjectType()) {
1282 // Handle the GNU void* extension.
1283 if (LHSPTy->getPointeeType()->isVoidType()) {
1284 Diag(loc, diag::ext_gnu_void_ptr,
1285 lex->getSourceRange(), rex->getSourceRange());
1286 } else {
1287 Diag(loc, diag::err_typecheck_sub_ptr_object,
1288 lex->getType().getAsString(), lex->getSourceRange());
1289 return QualType();
1290 }
1291 }
1292
1293 // The result type of a pointer-int computation is the pointer type.
1294 if (rex->getType()->isIntegerType())
1295 return lex->getType();
Steve Naroff94a5aca2007-07-16 22:23:01 +00001296
Chris Lattner4d62f422007-12-09 21:53:25 +00001297 // Handle pointer-pointer subtractions.
1298 if (const PointerType *RHSPTy = rex->getType()->getAsPointerType()) {
1299 // RHS must be an object type, unless void (GNU).
1300 if (!RHSPTy->getPointeeType()->isObjectType()) {
1301 // Handle the GNU void* extension.
1302 if (RHSPTy->getPointeeType()->isVoidType()) {
1303 if (!LHSPTy->getPointeeType()->isVoidType())
1304 Diag(loc, diag::ext_gnu_void_ptr,
1305 lex->getSourceRange(), rex->getSourceRange());
1306 } else {
1307 Diag(loc, diag::err_typecheck_sub_ptr_object,
1308 rex->getType().getAsString(), rex->getSourceRange());
1309 return QualType();
1310 }
1311 }
1312
1313 // Pointee types must be compatible.
1314 if (!Context.typesAreCompatible(LHSPTy->getPointeeType(),
1315 RHSPTy->getPointeeType())) {
1316 Diag(loc, diag::err_typecheck_sub_ptr_compatible,
1317 lex->getType().getAsString(), rex->getType().getAsString(),
1318 lex->getSourceRange(), rex->getSourceRange());
1319 return QualType();
1320 }
1321
1322 return Context.getPointerDiffType();
1323 }
1324 }
1325
Chris Lattner5c11c412007-12-12 05:47:28 +00001326 return InvalidOperands(loc, lex, rex);
Steve Naroff218bc2b2007-05-04 21:54:46 +00001327}
1328
1329inline QualType Sema::CheckShiftOperands( // C99 6.5.7
Chris Lattner5c11c412007-12-12 05:47:28 +00001330 Expr *&lex, Expr *&rex, SourceLocation loc, bool isCompAssign) {
1331 // C99 6.5.7p2: Each of the operands shall have integer type.
1332 if (!lex->getType()->isIntegerType() || !rex->getType()->isIntegerType())
1333 return InvalidOperands(loc, lex, rex);
Steve Naroff1926c832007-04-24 00:23:05 +00001334
Chris Lattner5c11c412007-12-12 05:47:28 +00001335 // Shifts don't perform usual arithmetic conversions, they just do integer
1336 // promotions on each operand. C99 6.5.7p3
Chris Lattner3c133402007-12-13 07:28:16 +00001337 if (!isCompAssign)
1338 UsualUnaryConversions(lex);
Chris Lattner5c11c412007-12-12 05:47:28 +00001339 UsualUnaryConversions(rex);
1340
1341 // "The type of the result is that of the promoted left operand."
1342 return lex->getType();
Steve Naroff26c8ea52007-03-21 21:08:52 +00001343}
1344
Chris Lattnerb620c342007-08-26 01:18:55 +00001345inline QualType Sema::CheckCompareOperands( // C99 6.5.8
1346 Expr *&lex, Expr *&rex, SourceLocation loc, bool isRelational)
Steve Naroff1926c832007-04-24 00:23:05 +00001347{
Chris Lattnerb620c342007-08-26 01:18:55 +00001348 // C99 6.5.8p3 / C99 6.5.9p4
Steve Naroff47fea352007-08-10 18:26:40 +00001349 if (lex->getType()->isArithmeticType() && rex->getType()->isArithmeticType())
1350 UsualArithmeticConversions(lex, rex);
1351 else {
1352 UsualUnaryConversions(lex);
1353 UsualUnaryConversions(rex);
1354 }
Steve Naroff31090012007-07-16 21:54:35 +00001355 QualType lType = lex->getType();
1356 QualType rType = rex->getType();
Steve Naroff1926c832007-04-24 00:23:05 +00001357
Ted Kremeneke2763b02007-10-29 17:13:39 +00001358 // For non-floating point types, check for self-comparisons of the form
1359 // x == x, x != x, x < x, etc. These always evaluate to a constant, and
1360 // often indicate logic errors in the program.
Ted Kremeneke451eae2007-10-29 16:58:49 +00001361 if (!lType->isFloatingType()) {
1362 if (DeclRefExpr* DRL = dyn_cast<DeclRefExpr>(IgnoreParen(lex)))
1363 if (DeclRefExpr* DRR = dyn_cast<DeclRefExpr>(IgnoreParen(rex)))
1364 if (DRL->getDecl() == DRR->getDecl())
1365 Diag(loc, diag::warn_selfcomparison);
1366 }
1367
Chris Lattnerb620c342007-08-26 01:18:55 +00001368 if (isRelational) {
1369 if (lType->isRealType() && rType->isRealType())
1370 return Context.IntTy;
1371 } else {
Ted Kremeneke2763b02007-10-29 17:13:39 +00001372 // Check for comparisons of floating point operands using != and ==.
Ted Kremeneke2763b02007-10-29 17:13:39 +00001373 if (lType->isFloatingType()) {
1374 assert (rType->isFloatingType());
Ted Kremenek43fb8b02007-11-25 00:58:00 +00001375 CheckFloatComparison(loc,lex,rex);
Ted Kremenekd4ecc6d2007-10-29 16:40:01 +00001376 }
1377
Chris Lattnerb620c342007-08-26 01:18:55 +00001378 if (lType->isArithmeticType() && rType->isArithmeticType())
1379 return Context.IntTy;
1380 }
Steve Naroffe4718892007-04-27 18:30:00 +00001381
Chris Lattner1895e582007-08-26 01:10:14 +00001382 bool LHSIsNull = lex->isNullPointerConstant(Context);
1383 bool RHSIsNull = rex->isNullPointerConstant(Context);
1384
Chris Lattnerb620c342007-08-26 01:18:55 +00001385 // All of the following pointer related warnings are GCC extensions, except
1386 // when handling null pointer constants. One day, we can consider making them
1387 // errors (when -pedantic-errors is enabled).
Steve Naroff808eb8f2007-08-27 04:08:11 +00001388 if (lType->isPointerType() && rType->isPointerType()) { // C99 6.5.8p2
Steve Naroffb6666252007-11-13 14:57:38 +00001389
1390 if (!LHSIsNull && !RHSIsNull && // C99 6.5.9p2
1391 !lType->getAsPointerType()->getPointeeType()->isVoidType() &&
1392 !rType->getAsPointerType()->getPointeeType()->isVoidType() &&
Steve Naroff32e44c02007-10-15 20:41:53 +00001393 !Context.pointerTypesAreCompatible(lType.getUnqualifiedType(),
1394 rType.getUnqualifiedType())) {
Steve Naroffcdee44c2007-08-16 21:48:38 +00001395 Diag(loc, diag::ext_typecheck_comparison_of_distinct_pointers,
1396 lType.getAsString(), rType.getAsString(),
1397 lex->getSourceRange(), rex->getSourceRange());
Steve Naroff75c17232007-06-13 21:41:08 +00001398 }
Chris Lattner1895e582007-08-26 01:10:14 +00001399 promoteExprToType(rex, lType); // promote the pointer to pointer
Steve Naroffcdee44c2007-08-16 21:48:38 +00001400 return Context.IntTy;
1401 }
Fariborz Jahanian134cbef2007-12-20 01:06:58 +00001402 if ((lType->isObjcQualifiedIdType() || rType->isObjcQualifiedIdType())
Fariborz Jahanianff7d2bf2007-12-21 00:33:59 +00001403 && Context.ObjcQualifiedIdTypesAreCompatible(lType, rType, true)) {
Fariborz Jahanian134cbef2007-12-20 01:06:58 +00001404 promoteExprToType(rex, lType);
1405 return Context.IntTy;
1406 }
Steve Naroffcdee44c2007-08-16 21:48:38 +00001407 if (lType->isPointerType() && rType->isIntegerType()) {
Chris Lattner1895e582007-08-26 01:10:14 +00001408 if (!RHSIsNull)
Steve Naroffcdee44c2007-08-16 21:48:38 +00001409 Diag(loc, diag::ext_typecheck_comparison_of_pointer_integer,
1410 lType.getAsString(), rType.getAsString(),
1411 lex->getSourceRange(), rex->getSourceRange());
Chris Lattner1895e582007-08-26 01:10:14 +00001412 promoteExprToType(rex, lType); // promote the integer to pointer
Steve Naroffcdee44c2007-08-16 21:48:38 +00001413 return Context.IntTy;
1414 }
1415 if (lType->isIntegerType() && rType->isPointerType()) {
Chris Lattner1895e582007-08-26 01:10:14 +00001416 if (!LHSIsNull)
Steve Naroffcdee44c2007-08-16 21:48:38 +00001417 Diag(loc, diag::ext_typecheck_comparison_of_pointer_integer,
1418 lType.getAsString(), rType.getAsString(),
1419 lex->getSourceRange(), rex->getSourceRange());
Chris Lattner1895e582007-08-26 01:10:14 +00001420 promoteExprToType(lex, rType); // promote the integer to pointer
Steve Naroffcdee44c2007-08-16 21:48:38 +00001421 return Context.IntTy;
Steve Naroff043d45d2007-05-15 02:32:35 +00001422 }
Chris Lattner5c11c412007-12-12 05:47:28 +00001423 return InvalidOperands(loc, lex, rex);
Steve Naroff26c8ea52007-03-21 21:08:52 +00001424}
1425
Steve Naroff218bc2b2007-05-04 21:54:46 +00001426inline QualType Sema::CheckBitwiseOperands(
Steve Naroffbe4c4d12007-08-24 19:07:16 +00001427 Expr *&lex, Expr *&rex, SourceLocation loc, bool isCompAssign)
Steve Naroff1926c832007-04-24 00:23:05 +00001428{
Steve Naroff94a5aca2007-07-16 22:23:01 +00001429 if (lex->getType()->isVectorType() || rex->getType()->isVectorType())
Steve Naroff84ff4b42007-07-09 21:31:10 +00001430 return CheckVectorOperands(loc, lex, rex);
Steve Naroffb8ea4fb2007-07-13 23:32:42 +00001431
Steve Naroffbe4c4d12007-08-24 19:07:16 +00001432 QualType compType = UsualArithmeticConversions(lex, rex, isCompAssign);
Steve Naroff1926c832007-04-24 00:23:05 +00001433
Steve Naroffdbd9e892007-07-17 00:58:39 +00001434 if (lex->getType()->isIntegerType() && rex->getType()->isIntegerType())
Steve Naroffbe4c4d12007-08-24 19:07:16 +00001435 return compType;
Chris Lattner5c11c412007-12-12 05:47:28 +00001436 return InvalidOperands(loc, lex, rex);
Steve Naroff26c8ea52007-03-21 21:08:52 +00001437}
1438
Steve Naroff218bc2b2007-05-04 21:54:46 +00001439inline QualType Sema::CheckLogicalOperands( // C99 6.5.[13,14]
Steve Naroff7a5af782007-07-13 16:58:59 +00001440 Expr *&lex, Expr *&rex, SourceLocation loc)
Steve Naroff1926c832007-04-24 00:23:05 +00001441{
Steve Naroff31090012007-07-16 21:54:35 +00001442 UsualUnaryConversions(lex);
1443 UsualUnaryConversions(rex);
Steve Naroffe4718892007-04-27 18:30:00 +00001444
Steve Naroffdbd9e892007-07-17 00:58:39 +00001445 if (lex->getType()->isScalarType() || rex->getType()->isScalarType())
Steve Naroff218bc2b2007-05-04 21:54:46 +00001446 return Context.IntTy;
Chris Lattner5c11c412007-12-12 05:47:28 +00001447 return InvalidOperands(loc, lex, rex);
Steve Naroffae4143e2007-04-26 20:39:23 +00001448}
1449
Steve Naroff35d85152007-05-07 00:24:15 +00001450inline QualType Sema::CheckAssignmentOperands( // C99 6.5.16.1
Steve Naroff0c1c7ed2007-08-24 22:33:52 +00001451 Expr *lex, Expr *&rex, SourceLocation loc, QualType compoundType)
Steve Naroffae4143e2007-04-26 20:39:23 +00001452{
Steve Naroff0af91202007-04-27 21:51:21 +00001453 QualType lhsType = lex->getType();
Steve Naroff35d85152007-05-07 00:24:15 +00001454 QualType rhsType = compoundType.isNull() ? rex->getType() : compoundType;
Steve Naroff1f4d7272007-05-11 04:00:31 +00001455 bool hadError = false;
Steve Naroff9358c712007-05-27 23:58:33 +00001456 Expr::isModifiableLvalueResult mlval = lex->isModifiableLvalue();
1457
1458 switch (mlval) { // C99 6.5.16p2
1459 case Expr::MLV_Valid:
1460 break;
1461 case Expr::MLV_ConstQualified:
1462 Diag(loc, diag::err_typecheck_assign_const, lex->getSourceRange());
1463 hadError = true;
1464 break;
1465 case Expr::MLV_ArrayType:
1466 Diag(loc, diag::err_typecheck_array_not_modifiable_lvalue,
1467 lhsType.getAsString(), lex->getSourceRange());
1468 return QualType();
1469 case Expr::MLV_NotObjectType:
1470 Diag(loc, diag::err_typecheck_non_object_not_modifiable_lvalue,
1471 lhsType.getAsString(), lex->getSourceRange());
1472 return QualType();
1473 case Expr::MLV_InvalidExpression:
1474 Diag(loc, diag::err_typecheck_expression_not_modifiable_lvalue,
1475 lex->getSourceRange());
1476 return QualType();
1477 case Expr::MLV_IncompleteType:
1478 case Expr::MLV_IncompleteVoidType:
1479 Diag(loc, diag::err_typecheck_incomplete_type_not_modifiable_lvalue,
1480 lhsType.getAsString(), lex->getSourceRange());
1481 return QualType();
Steve Naroff0d595ca2007-07-30 03:29:09 +00001482 case Expr::MLV_DuplicateVectorComponents:
1483 Diag(loc, diag::err_typecheck_duplicate_vector_components_not_mlvalue,
1484 lex->getSourceRange());
1485 return QualType();
Steve Naroff218bc2b2007-05-04 21:54:46 +00001486 }
Steve Naroffb8ea4fb2007-07-13 23:32:42 +00001487 AssignmentCheckResult result;
1488
1489 if (compoundType.isNull())
1490 result = CheckSingleAssignmentConstraints(lhsType, rex);
1491 else
1492 result = CheckCompoundAssignmentConstraints(lhsType, rhsType);
Steve Naroffad373bd2007-07-31 12:34:36 +00001493
Steve Naroff218bc2b2007-05-04 21:54:46 +00001494 // decode the result (notice that extensions still return a type).
1495 switch (result) {
1496 case Compatible:
Steve Naroff1f4d7272007-05-11 04:00:31 +00001497 break;
Steve Naroff218bc2b2007-05-04 21:54:46 +00001498 case Incompatible:
Steve Naroffe845e272007-05-18 01:06:45 +00001499 Diag(loc, diag::err_typecheck_assign_incompatible,
Chris Lattner84e160a2007-05-19 07:03:17 +00001500 lhsType.getAsString(), rhsType.getAsString(),
1501 lex->getSourceRange(), rex->getSourceRange());
Steve Naroff1f4d7272007-05-11 04:00:31 +00001502 hadError = true;
1503 break;
Steve Naroff218bc2b2007-05-04 21:54:46 +00001504 case PointerFromInt:
Steve Naroff0ee0b0a2007-11-27 17:58:44 +00001505 Diag(loc, diag::ext_typecheck_assign_pointer_int,
1506 lhsType.getAsString(), rhsType.getAsString(),
1507 lex->getSourceRange(), rex->getSourceRange());
Steve Naroff1f4d7272007-05-11 04:00:31 +00001508 break;
Steve Naroff56faab22007-05-30 04:20:12 +00001509 case IntFromPointer:
1510 Diag(loc, diag::ext_typecheck_assign_pointer_int,
1511 lhsType.getAsString(), rhsType.getAsString(),
Steve Naroff9358c712007-05-27 23:58:33 +00001512 lex->getSourceRange(), rex->getSourceRange());
Steve Naroff1f4d7272007-05-11 04:00:31 +00001513 break;
Steve Naroff218bc2b2007-05-04 21:54:46 +00001514 case IncompatiblePointer:
Steve Naroff9358c712007-05-27 23:58:33 +00001515 Diag(loc, diag::ext_typecheck_assign_incompatible_pointer,
1516 lhsType.getAsString(), rhsType.getAsString(),
1517 lex->getSourceRange(), rex->getSourceRange());
Steve Naroff1f4d7272007-05-11 04:00:31 +00001518 break;
1519 case CompatiblePointerDiscardsQualifiers:
Steve Naroff9358c712007-05-27 23:58:33 +00001520 Diag(loc, diag::ext_typecheck_assign_discards_qualifiers,
1521 lhsType.getAsString(), rhsType.getAsString(),
1522 lex->getSourceRange(), rex->getSourceRange());
Steve Naroff1f4d7272007-05-11 04:00:31 +00001523 break;
Steve Naroff218bc2b2007-05-04 21:54:46 +00001524 }
Steve Naroff98cf3e92007-06-06 18:38:38 +00001525 // C99 6.5.16p3: The type of an assignment expression is the type of the
1526 // left operand unless the left operand has qualified type, in which case
1527 // it is the unqualified version of the type of the left operand.
1528 // C99 6.5.16.1p2: In simple assignment, the value of the right operand
1529 // is converted to the type of the assignment expression (above).
Chris Lattnerf17bd422007-08-30 17:45:32 +00001530 // C++ 5.17p1: the type of the assignment expression is that of its left
1531 // oprdu.
Steve Naroff98cf3e92007-06-06 18:38:38 +00001532 return hadError ? QualType() : lhsType.getUnqualifiedType();
Steve Naroffae4143e2007-04-26 20:39:23 +00001533}
1534
Steve Naroff218bc2b2007-05-04 21:54:46 +00001535inline QualType Sema::CheckCommaOperands( // C99 6.5.17
Steve Naroff7a5af782007-07-13 16:58:59 +00001536 Expr *&lex, Expr *&rex, SourceLocation loc) {
Steve Naroff31090012007-07-16 21:54:35 +00001537 UsualUnaryConversions(rex);
1538 return rex->getType();
Steve Naroff95af0132007-03-30 23:47:58 +00001539}
1540
Steve Naroff7a5af782007-07-13 16:58:59 +00001541/// CheckIncrementDecrementOperand - unlike most "Check" methods, this routine
1542/// doesn't need to call UsualUnaryConversions or UsualArithmeticConversions.
Steve Naroff71ce2e02007-05-18 22:53:50 +00001543QualType Sema::CheckIncrementDecrementOperand(Expr *op, SourceLocation OpLoc) {
Steve Naroff7a5af782007-07-13 16:58:59 +00001544 QualType resType = op->getType();
Steve Naroff35d85152007-05-07 00:24:15 +00001545 assert(!resType.isNull() && "no type for increment/decrement expression");
Steve Naroffd50c88e2007-04-05 21:15:20 +00001546
Steve Naroff9d139172007-08-24 17:20:07 +00001547 // C99 6.5.2.4p1: We allow complex as a GCC extension.
Steve Naroff5f9ae642007-11-11 14:15:57 +00001548 if (const PointerType *pt = resType->getAsPointerType()) {
Steve Naroff35d85152007-05-07 00:24:15 +00001549 if (!pt->getPointeeType()->isObjectType()) { // C99 6.5.2.4p2, 6.5.6p2
Steve Naroff71ce2e02007-05-18 22:53:50 +00001550 Diag(OpLoc, diag::err_typecheck_arithmetic_incomplete_type,
1551 resType.getAsString(), op->getSourceRange());
Steve Naroff35d85152007-05-07 00:24:15 +00001552 return QualType();
1553 }
Steve Naroff9d139172007-08-24 17:20:07 +00001554 } else if (!resType->isRealType()) {
1555 if (resType->isComplexType())
1556 // C99 does not support ++/-- on complex types.
1557 Diag(OpLoc, diag::ext_integer_increment_complex,
1558 resType.getAsString(), op->getSourceRange());
1559 else {
1560 Diag(OpLoc, diag::err_typecheck_illegal_increment_decrement,
1561 resType.getAsString(), op->getSourceRange());
1562 return QualType();
1563 }
Steve Naroff46ba1eb2007-04-03 23:13:13 +00001564 }
Steve Naroff9e1e5512007-08-23 21:37:33 +00001565 // At this point, we know we have a real, complex or pointer type.
1566 // Now make sure the operand is a modifiable lvalue.
Steve Naroff9358c712007-05-27 23:58:33 +00001567 Expr::isModifiableLvalueResult mlval = op->isModifiableLvalue();
1568 if (mlval != Expr::MLV_Valid) {
1569 // FIXME: emit a more precise diagnostic...
Steve Naroff71ce2e02007-05-18 22:53:50 +00001570 Diag(OpLoc, diag::err_typecheck_invalid_lvalue_incr_decr,
Chris Lattner84e160a2007-05-19 07:03:17 +00001571 op->getSourceRange());
Steve Naroff35d85152007-05-07 00:24:15 +00001572 return QualType();
1573 }
1574 return resType;
Steve Naroff26c8ea52007-03-21 21:08:52 +00001575}
1576
Steve Naroff1926c832007-04-24 00:23:05 +00001577/// getPrimaryDeclaration - Helper function for CheckAddressOfOperand().
Steve Naroff47500512007-04-19 23:00:49 +00001578/// This routine allows us to typecheck complex/recursive expressions
1579/// where the declaration is needed for type checking. Here are some
1580/// examples: &s.xx, &s.zz[1].yy, &(1+2), &(XX), &"123"[2].
Steve Naroff1926c832007-04-24 00:23:05 +00001581static Decl *getPrimaryDeclaration(Expr *e) {
Steve Naroff47500512007-04-19 23:00:49 +00001582 switch (e->getStmtClass()) {
1583 case Stmt::DeclRefExprClass:
1584 return cast<DeclRefExpr>(e)->getDecl();
1585 case Stmt::MemberExprClass:
Chris Lattner48d52842007-11-16 17:46:48 +00001586 // Fields cannot be declared with a 'register' storage class.
1587 // &X->f is always ok, even if X is declared register.
1588 if (cast<MemberExpr>(e)->isArrow())
1589 return 0;
Steve Naroff1926c832007-04-24 00:23:05 +00001590 return getPrimaryDeclaration(cast<MemberExpr>(e)->getBase());
Steve Naroff47500512007-04-19 23:00:49 +00001591 case Stmt::ArraySubscriptExprClass:
Chris Lattner48d52842007-11-16 17:46:48 +00001592 // &X[4] and &4[X] is invalid if X is invalid.
Steve Naroff1926c832007-04-24 00:23:05 +00001593 return getPrimaryDeclaration(cast<ArraySubscriptExpr>(e)->getBase());
Steve Naroff47500512007-04-19 23:00:49 +00001594 case Stmt::UnaryOperatorClass:
Steve Naroff1926c832007-04-24 00:23:05 +00001595 return getPrimaryDeclaration(cast<UnaryOperator>(e)->getSubExpr());
Steve Naroff47500512007-04-19 23:00:49 +00001596 case Stmt::ParenExprClass:
Steve Naroff1926c832007-04-24 00:23:05 +00001597 return getPrimaryDeclaration(cast<ParenExpr>(e)->getSubExpr());
Chris Lattner48d52842007-11-16 17:46:48 +00001598 case Stmt::ImplicitCastExprClass:
1599 // &X[4] when X is an array, has an implicit cast from array to pointer.
1600 return getPrimaryDeclaration(cast<ImplicitCastExpr>(e)->getSubExpr());
Steve Naroff47500512007-04-19 23:00:49 +00001601 default:
1602 return 0;
1603 }
1604}
1605
1606/// CheckAddressOfOperand - The operand of & must be either a function
1607/// designator or an lvalue designating an object. If it is an lvalue, the
1608/// object cannot be declared with storage class register or be a bit field.
1609/// Note: The usual conversions are *not* applied to the operand of the &
Steve Naroffa78fe7e2007-05-16 19:47:19 +00001610/// operator (C99 6.3.2.1p[2-4]), and its result is never an lvalue.
Steve Naroff35d85152007-05-07 00:24:15 +00001611QualType Sema::CheckAddressOfOperand(Expr *op, SourceLocation OpLoc) {
Steve Naroff1926c832007-04-24 00:23:05 +00001612 Decl *dcl = getPrimaryDeclaration(op);
Steve Naroff9358c712007-05-27 23:58:33 +00001613 Expr::isLvalueResult lval = op->isLvalue();
Steve Naroff47500512007-04-19 23:00:49 +00001614
Steve Naroff9358c712007-05-27 23:58:33 +00001615 if (lval != Expr::LV_Valid) { // C99 6.5.3.2p1
Chris Lattner48d52842007-11-16 17:46:48 +00001616 if (!dcl || !isa<FunctionDecl>(dcl)) {// allow function designators
1617 // FIXME: emit more specific diag...
Steve Naroff71ce2e02007-05-18 22:53:50 +00001618 Diag(OpLoc, diag::err_typecheck_invalid_lvalue_addrof,
1619 op->getSourceRange());
Steve Naroff35d85152007-05-07 00:24:15 +00001620 return QualType();
1621 }
Steve Naroff47500512007-04-19 23:00:49 +00001622 } else if (dcl) {
1623 // We have an lvalue with a decl. Make sure the decl is not declared
1624 // with the register storage-class specifier.
1625 if (const VarDecl *vd = dyn_cast<VarDecl>(dcl)) {
Steve Naroff35d85152007-05-07 00:24:15 +00001626 if (vd->getStorageClass() == VarDecl::Register) {
Steve Naroff71ce2e02007-05-18 22:53:50 +00001627 Diag(OpLoc, diag::err_typecheck_address_of_register,
Chris Lattner84e160a2007-05-19 07:03:17 +00001628 op->getSourceRange());
Steve Naroff35d85152007-05-07 00:24:15 +00001629 return QualType();
1630 }
Steve Narofff633d092007-04-25 19:01:39 +00001631 } else
1632 assert(0 && "Unknown/unexpected decl type");
1633
Steve Naroff47500512007-04-19 23:00:49 +00001634 // FIXME: add check for bitfields!
1635 }
1636 // If the operand has type "type", the result has type "pointer to type".
Steve Naroff35d85152007-05-07 00:24:15 +00001637 return Context.getPointerType(op->getType());
Steve Naroff47500512007-04-19 23:00:49 +00001638}
1639
Steve Naroff35d85152007-05-07 00:24:15 +00001640QualType Sema::CheckIndirectionOperand(Expr *op, SourceLocation OpLoc) {
Steve Naroff31090012007-07-16 21:54:35 +00001641 UsualUnaryConversions(op);
1642 QualType qType = op->getType();
Steve Naroff35d85152007-05-07 00:24:15 +00001643
Chris Lattnerc996b172007-07-31 16:53:04 +00001644 if (const PointerType *PT = qType->getAsPointerType()) {
Steve Naroff758ada12007-05-28 16:15:57 +00001645 QualType ptype = PT->getPointeeType();
1646 // C99 6.5.3.2p4. "if it points to an object,...".
1647 if (ptype->isIncompleteType()) { // An incomplete type is not an object
1648 // GCC compat: special case 'void *' (treat as warning).
1649 if (ptype->isVoidType()) {
1650 Diag(OpLoc, diag::ext_typecheck_deref_ptr_to_void,
Steve Naroffeb9da942007-05-30 00:06:37 +00001651 qType.getAsString(), op->getSourceRange());
Steve Naroff758ada12007-05-28 16:15:57 +00001652 } else {
1653 Diag(OpLoc, diag::err_typecheck_deref_incomplete_type,
Steve Naroffeb9da942007-05-30 00:06:37 +00001654 ptype.getAsString(), op->getSourceRange());
Steve Naroff758ada12007-05-28 16:15:57 +00001655 return QualType();
1656 }
1657 }
1658 return ptype;
1659 }
1660 Diag(OpLoc, diag::err_typecheck_indirection_requires_pointer,
Steve Naroffeb9da942007-05-30 00:06:37 +00001661 qType.getAsString(), op->getSourceRange());
Steve Naroff35d85152007-05-07 00:24:15 +00001662 return QualType();
Steve Naroff1926c832007-04-24 00:23:05 +00001663}
Steve Naroff218bc2b2007-05-04 21:54:46 +00001664
1665static inline BinaryOperator::Opcode ConvertTokenKindToBinaryOpcode(
1666 tok::TokenKind Kind) {
1667 BinaryOperator::Opcode Opc;
1668 switch (Kind) {
1669 default: assert(0 && "Unknown binop!");
1670 case tok::star: Opc = BinaryOperator::Mul; break;
1671 case tok::slash: Opc = BinaryOperator::Div; break;
1672 case tok::percent: Opc = BinaryOperator::Rem; break;
1673 case tok::plus: Opc = BinaryOperator::Add; break;
1674 case tok::minus: Opc = BinaryOperator::Sub; break;
1675 case tok::lessless: Opc = BinaryOperator::Shl; break;
1676 case tok::greatergreater: Opc = BinaryOperator::Shr; break;
1677 case tok::lessequal: Opc = BinaryOperator::LE; break;
1678 case tok::less: Opc = BinaryOperator::LT; break;
1679 case tok::greaterequal: Opc = BinaryOperator::GE; break;
1680 case tok::greater: Opc = BinaryOperator::GT; break;
1681 case tok::exclaimequal: Opc = BinaryOperator::NE; break;
1682 case tok::equalequal: Opc = BinaryOperator::EQ; break;
1683 case tok::amp: Opc = BinaryOperator::And; break;
1684 case tok::caret: Opc = BinaryOperator::Xor; break;
1685 case tok::pipe: Opc = BinaryOperator::Or; break;
1686 case tok::ampamp: Opc = BinaryOperator::LAnd; break;
1687 case tok::pipepipe: Opc = BinaryOperator::LOr; break;
1688 case tok::equal: Opc = BinaryOperator::Assign; break;
1689 case tok::starequal: Opc = BinaryOperator::MulAssign; break;
1690 case tok::slashequal: Opc = BinaryOperator::DivAssign; break;
1691 case tok::percentequal: Opc = BinaryOperator::RemAssign; break;
1692 case tok::plusequal: Opc = BinaryOperator::AddAssign; break;
1693 case tok::minusequal: Opc = BinaryOperator::SubAssign; break;
1694 case tok::lesslessequal: Opc = BinaryOperator::ShlAssign; break;
1695 case tok::greatergreaterequal: Opc = BinaryOperator::ShrAssign; break;
1696 case tok::ampequal: Opc = BinaryOperator::AndAssign; break;
1697 case tok::caretequal: Opc = BinaryOperator::XorAssign; break;
1698 case tok::pipeequal: Opc = BinaryOperator::OrAssign; break;
1699 case tok::comma: Opc = BinaryOperator::Comma; break;
1700 }
1701 return Opc;
1702}
1703
Steve Naroff35d85152007-05-07 00:24:15 +00001704static inline UnaryOperator::Opcode ConvertTokenKindToUnaryOpcode(
1705 tok::TokenKind Kind) {
1706 UnaryOperator::Opcode Opc;
1707 switch (Kind) {
1708 default: assert(0 && "Unknown unary op!");
1709 case tok::plusplus: Opc = UnaryOperator::PreInc; break;
1710 case tok::minusminus: Opc = UnaryOperator::PreDec; break;
1711 case tok::amp: Opc = UnaryOperator::AddrOf; break;
1712 case tok::star: Opc = UnaryOperator::Deref; break;
1713 case tok::plus: Opc = UnaryOperator::Plus; break;
1714 case tok::minus: Opc = UnaryOperator::Minus; break;
1715 case tok::tilde: Opc = UnaryOperator::Not; break;
1716 case tok::exclaim: Opc = UnaryOperator::LNot; break;
1717 case tok::kw_sizeof: Opc = UnaryOperator::SizeOf; break;
1718 case tok::kw___alignof: Opc = UnaryOperator::AlignOf; break;
1719 case tok::kw___real: Opc = UnaryOperator::Real; break;
1720 case tok::kw___imag: Opc = UnaryOperator::Imag; break;
Chris Lattnerd0f76512007-06-08 22:16:53 +00001721 case tok::kw___extension__: Opc = UnaryOperator::Extension; break;
Steve Naroff35d85152007-05-07 00:24:15 +00001722 }
1723 return Opc;
1724}
1725
Steve Naroff218bc2b2007-05-04 21:54:46 +00001726// Binary Operators. 'Tok' is the token for the operator.
Steve Naroff83895f72007-09-16 03:34:24 +00001727Action::ExprResult Sema::ActOnBinOp(SourceLocation TokLoc, tok::TokenKind Kind,
Steve Naroff218bc2b2007-05-04 21:54:46 +00001728 ExprTy *LHS, ExprTy *RHS) {
1729 BinaryOperator::Opcode Opc = ConvertTokenKindToBinaryOpcode(Kind);
1730 Expr *lhs = (Expr *)LHS, *rhs = (Expr*)RHS;
1731
Steve Naroff83895f72007-09-16 03:34:24 +00001732 assert((lhs != 0) && "ActOnBinOp(): missing left expression");
1733 assert((rhs != 0) && "ActOnBinOp(): missing right expression");
Steve Naroff218bc2b2007-05-04 21:54:46 +00001734
Chris Lattner256c21b2007-06-28 03:53:10 +00001735 QualType ResultTy; // Result type of the binary operator.
1736 QualType CompTy; // Computation type for compound assignments (e.g. '+=')
Steve Naroff218bc2b2007-05-04 21:54:46 +00001737
1738 switch (Opc) {
1739 default:
1740 assert(0 && "Unknown binary expr!");
1741 case BinaryOperator::Assign:
Chris Lattner256c21b2007-06-28 03:53:10 +00001742 ResultTy = CheckAssignmentOperands(lhs, rhs, TokLoc, QualType());
Steve Naroff218bc2b2007-05-04 21:54:46 +00001743 break;
1744 case BinaryOperator::Mul:
1745 case BinaryOperator::Div:
Chris Lattner256c21b2007-06-28 03:53:10 +00001746 ResultTy = CheckMultiplyDivideOperands(lhs, rhs, TokLoc);
Steve Naroff218bc2b2007-05-04 21:54:46 +00001747 break;
1748 case BinaryOperator::Rem:
Chris Lattner256c21b2007-06-28 03:53:10 +00001749 ResultTy = CheckRemainderOperands(lhs, rhs, TokLoc);
Steve Naroff218bc2b2007-05-04 21:54:46 +00001750 break;
1751 case BinaryOperator::Add:
Chris Lattner256c21b2007-06-28 03:53:10 +00001752 ResultTy = CheckAdditionOperands(lhs, rhs, TokLoc);
Steve Naroff218bc2b2007-05-04 21:54:46 +00001753 break;
1754 case BinaryOperator::Sub:
Chris Lattner256c21b2007-06-28 03:53:10 +00001755 ResultTy = CheckSubtractionOperands(lhs, rhs, TokLoc);
Steve Naroff218bc2b2007-05-04 21:54:46 +00001756 break;
1757 case BinaryOperator::Shl:
1758 case BinaryOperator::Shr:
Chris Lattner256c21b2007-06-28 03:53:10 +00001759 ResultTy = CheckShiftOperands(lhs, rhs, TokLoc);
Steve Naroff218bc2b2007-05-04 21:54:46 +00001760 break;
1761 case BinaryOperator::LE:
1762 case BinaryOperator::LT:
1763 case BinaryOperator::GE:
1764 case BinaryOperator::GT:
Chris Lattnerb620c342007-08-26 01:18:55 +00001765 ResultTy = CheckCompareOperands(lhs, rhs, TokLoc, true);
Steve Naroff218bc2b2007-05-04 21:54:46 +00001766 break;
1767 case BinaryOperator::EQ:
1768 case BinaryOperator::NE:
Chris Lattnerb620c342007-08-26 01:18:55 +00001769 ResultTy = CheckCompareOperands(lhs, rhs, TokLoc, false);
Steve Naroff218bc2b2007-05-04 21:54:46 +00001770 break;
1771 case BinaryOperator::And:
1772 case BinaryOperator::Xor:
1773 case BinaryOperator::Or:
Chris Lattner256c21b2007-06-28 03:53:10 +00001774 ResultTy = CheckBitwiseOperands(lhs, rhs, TokLoc);
Steve Naroff218bc2b2007-05-04 21:54:46 +00001775 break;
1776 case BinaryOperator::LAnd:
1777 case BinaryOperator::LOr:
Chris Lattner256c21b2007-06-28 03:53:10 +00001778 ResultTy = CheckLogicalOperands(lhs, rhs, TokLoc);
Steve Naroff218bc2b2007-05-04 21:54:46 +00001779 break;
1780 case BinaryOperator::MulAssign:
1781 case BinaryOperator::DivAssign:
Steve Naroffbe4c4d12007-08-24 19:07:16 +00001782 CompTy = CheckMultiplyDivideOperands(lhs, rhs, TokLoc, true);
Chris Lattner256c21b2007-06-28 03:53:10 +00001783 if (!CompTy.isNull())
1784 ResultTy = CheckAssignmentOperands(lhs, rhs, TokLoc, CompTy);
Steve Naroff218bc2b2007-05-04 21:54:46 +00001785 break;
1786 case BinaryOperator::RemAssign:
Steve Naroffbe4c4d12007-08-24 19:07:16 +00001787 CompTy = CheckRemainderOperands(lhs, rhs, TokLoc, true);
Chris Lattner256c21b2007-06-28 03:53:10 +00001788 if (!CompTy.isNull())
1789 ResultTy = CheckAssignmentOperands(lhs, rhs, TokLoc, CompTy);
Steve Naroff218bc2b2007-05-04 21:54:46 +00001790 break;
1791 case BinaryOperator::AddAssign:
Steve Naroffbe4c4d12007-08-24 19:07:16 +00001792 CompTy = CheckAdditionOperands(lhs, rhs, TokLoc, true);
Chris Lattner256c21b2007-06-28 03:53:10 +00001793 if (!CompTy.isNull())
1794 ResultTy = CheckAssignmentOperands(lhs, rhs, TokLoc, CompTy);
Steve Naroff218bc2b2007-05-04 21:54:46 +00001795 break;
1796 case BinaryOperator::SubAssign:
Steve Naroffbe4c4d12007-08-24 19:07:16 +00001797 CompTy = CheckSubtractionOperands(lhs, rhs, TokLoc, true);
Chris Lattner256c21b2007-06-28 03:53:10 +00001798 if (!CompTy.isNull())
1799 ResultTy = CheckAssignmentOperands(lhs, rhs, TokLoc, CompTy);
Steve Naroff218bc2b2007-05-04 21:54:46 +00001800 break;
1801 case BinaryOperator::ShlAssign:
1802 case BinaryOperator::ShrAssign:
Steve Naroffbe4c4d12007-08-24 19:07:16 +00001803 CompTy = CheckShiftOperands(lhs, rhs, TokLoc, true);
Chris Lattner256c21b2007-06-28 03:53:10 +00001804 if (!CompTy.isNull())
1805 ResultTy = CheckAssignmentOperands(lhs, rhs, TokLoc, CompTy);
Steve Naroff218bc2b2007-05-04 21:54:46 +00001806 break;
1807 case BinaryOperator::AndAssign:
1808 case BinaryOperator::XorAssign:
1809 case BinaryOperator::OrAssign:
Steve Naroffbe4c4d12007-08-24 19:07:16 +00001810 CompTy = CheckBitwiseOperands(lhs, rhs, TokLoc, true);
Chris Lattner256c21b2007-06-28 03:53:10 +00001811 if (!CompTy.isNull())
1812 ResultTy = CheckAssignmentOperands(lhs, rhs, TokLoc, CompTy);
Steve Naroff218bc2b2007-05-04 21:54:46 +00001813 break;
1814 case BinaryOperator::Comma:
Chris Lattner256c21b2007-06-28 03:53:10 +00001815 ResultTy = CheckCommaOperands(lhs, rhs, TokLoc);
Steve Naroff218bc2b2007-05-04 21:54:46 +00001816 break;
1817 }
Chris Lattner256c21b2007-06-28 03:53:10 +00001818 if (ResultTy.isNull())
Steve Naroff218bc2b2007-05-04 21:54:46 +00001819 return true;
Chris Lattner256c21b2007-06-28 03:53:10 +00001820 if (CompTy.isNull())
Chris Lattnerc11005f2007-08-28 18:36:55 +00001821 return new BinaryOperator(lhs, rhs, Opc, ResultTy, TokLoc);
Chris Lattner256c21b2007-06-28 03:53:10 +00001822 else
Chris Lattnerc11005f2007-08-28 18:36:55 +00001823 return new CompoundAssignOperator(lhs, rhs, Opc, ResultTy, CompTy, TokLoc);
Steve Naroff218bc2b2007-05-04 21:54:46 +00001824}
1825
Steve Naroff35d85152007-05-07 00:24:15 +00001826// Unary Operators. 'Tok' is the token for the operator.
Steve Naroff83895f72007-09-16 03:34:24 +00001827Action::ExprResult Sema::ActOnUnaryOp(SourceLocation OpLoc, tok::TokenKind Op,
Chris Lattner86554282007-06-08 22:32:33 +00001828 ExprTy *input) {
1829 Expr *Input = (Expr*)input;
Steve Naroff35d85152007-05-07 00:24:15 +00001830 UnaryOperator::Opcode Opc = ConvertTokenKindToUnaryOpcode(Op);
1831 QualType resultType;
1832 switch (Opc) {
1833 default:
1834 assert(0 && "Unimplemented unary expr!");
1835 case UnaryOperator::PreInc:
1836 case UnaryOperator::PreDec:
Chris Lattner86554282007-06-08 22:32:33 +00001837 resultType = CheckIncrementDecrementOperand(Input, OpLoc);
Steve Naroff35d85152007-05-07 00:24:15 +00001838 break;
1839 case UnaryOperator::AddrOf:
Chris Lattner86554282007-06-08 22:32:33 +00001840 resultType = CheckAddressOfOperand(Input, OpLoc);
Steve Naroff35d85152007-05-07 00:24:15 +00001841 break;
1842 case UnaryOperator::Deref:
Steve Naroffb7235642007-12-18 04:06:57 +00001843 DefaultFunctionArrayConversion(Input);
Chris Lattner86554282007-06-08 22:32:33 +00001844 resultType = CheckIndirectionOperand(Input, OpLoc);
Steve Naroff35d85152007-05-07 00:24:15 +00001845 break;
1846 case UnaryOperator::Plus:
1847 case UnaryOperator::Minus:
Steve Naroff31090012007-07-16 21:54:35 +00001848 UsualUnaryConversions(Input);
1849 resultType = Input->getType();
Steve Naroff35d85152007-05-07 00:24:15 +00001850 if (!resultType->isArithmeticType()) // C99 6.5.3.3p1
Chris Lattnerc04bd6a2007-05-16 18:09:54 +00001851 return Diag(OpLoc, diag::err_typecheck_unary_expr,
1852 resultType.getAsString());
Steve Naroff35d85152007-05-07 00:24:15 +00001853 break;
1854 case UnaryOperator::Not: // bitwise complement
Steve Naroff31090012007-07-16 21:54:35 +00001855 UsualUnaryConversions(Input);
1856 resultType = Input->getType();
Steve Naroff9d139172007-08-24 17:20:07 +00001857 // C99 6.5.3.3p1. We allow complex as a GCC extension.
1858 if (!resultType->isIntegerType()) {
1859 if (resultType->isComplexType())
1860 // C99 does not support '~' for complex conjugation.
1861 Diag(OpLoc, diag::ext_integer_complement_complex,
1862 resultType.getAsString());
1863 else
1864 return Diag(OpLoc, diag::err_typecheck_unary_expr,
1865 resultType.getAsString());
1866 }
Steve Naroff35d85152007-05-07 00:24:15 +00001867 break;
1868 case UnaryOperator::LNot: // logical negation
Steve Naroff71b59a92007-06-04 22:22:31 +00001869 // Unlike +/-/~, integer promotions aren't done here (C99 6.5.3.3p5).
Steve Naroff31090012007-07-16 21:54:35 +00001870 DefaultFunctionArrayConversion(Input);
1871 resultType = Input->getType();
Steve Naroff35d85152007-05-07 00:24:15 +00001872 if (!resultType->isScalarType()) // C99 6.5.3.3p1
Chris Lattnerc04bd6a2007-05-16 18:09:54 +00001873 return Diag(OpLoc, diag::err_typecheck_unary_expr,
1874 resultType.getAsString());
Chris Lattnerbe31ed82007-06-02 19:11:33 +00001875 // LNot always has type int. C99 6.5.3.3p5.
1876 resultType = Context.IntTy;
Steve Naroff35d85152007-05-07 00:24:15 +00001877 break;
1878 case UnaryOperator::SizeOf:
Chris Lattner86554282007-06-08 22:32:33 +00001879 resultType = CheckSizeOfAlignOfOperand(Input->getType(), OpLoc, true);
Steve Naroff043d45d2007-05-15 02:32:35 +00001880 break;
Steve Naroff35d85152007-05-07 00:24:15 +00001881 case UnaryOperator::AlignOf:
Chris Lattner86554282007-06-08 22:32:33 +00001882 resultType = CheckSizeOfAlignOfOperand(Input->getType(), OpLoc, false);
1883 break;
Chris Lattner30b5dd02007-08-24 21:16:53 +00001884 case UnaryOperator::Real:
Chris Lattner30b5dd02007-08-24 21:16:53 +00001885 case UnaryOperator::Imag:
Chris Lattner74ed76b2007-08-24 21:41:10 +00001886 resultType = CheckRealImagOperand(Input, OpLoc);
Chris Lattner30b5dd02007-08-24 21:16:53 +00001887 break;
Chris Lattner86554282007-06-08 22:32:33 +00001888 case UnaryOperator::Extension:
Chris Lattner86554282007-06-08 22:32:33 +00001889 resultType = Input->getType();
Steve Naroff043d45d2007-05-15 02:32:35 +00001890 break;
Steve Naroff35d85152007-05-07 00:24:15 +00001891 }
1892 if (resultType.isNull())
1893 return true;
Chris Lattner86554282007-06-08 22:32:33 +00001894 return new UnaryOperator(Input, Opc, resultType, OpLoc);
Steve Naroff35d85152007-05-07 00:24:15 +00001895}
Chris Lattnereefa10e2007-05-28 06:56:27 +00001896
Steve Naroff66356bd2007-09-16 14:56:35 +00001897/// ActOnAddrLabel - Parse the GNU address of label extension: "&&foo".
1898Sema::ExprResult Sema::ActOnAddrLabel(SourceLocation OpLoc,
Chris Lattnereefa10e2007-05-28 06:56:27 +00001899 SourceLocation LabLoc,
1900 IdentifierInfo *LabelII) {
1901 // Look up the record for this label identifier.
1902 LabelStmt *&LabelDecl = LabelMap[LabelII];
1903
1904 // If we haven't seen this label yet, create a forward reference.
1905 if (LabelDecl == 0)
1906 LabelDecl = new LabelStmt(LabLoc, LabelII, 0);
1907
1908 // Create the AST node. The address of a label always has type 'void*'.
Chris Lattnerd268a7a2007-08-03 17:31:20 +00001909 return new AddrLabelExpr(OpLoc, LabLoc, LabelDecl,
1910 Context.getPointerType(Context.VoidTy));
Chris Lattnereefa10e2007-05-28 06:56:27 +00001911}
1912
Steve Naroff66356bd2007-09-16 14:56:35 +00001913Sema::ExprResult Sema::ActOnStmtExpr(SourceLocation LPLoc, StmtTy *substmt,
Chris Lattner366727f2007-07-24 16:58:17 +00001914 SourceLocation RPLoc) { // "({..})"
1915 Stmt *SubStmt = static_cast<Stmt*>(substmt);
1916 assert(SubStmt && isa<CompoundStmt>(SubStmt) && "Invalid action invocation!");
1917 CompoundStmt *Compound = cast<CompoundStmt>(SubStmt);
1918
1919 // FIXME: there are a variety of strange constraints to enforce here, for
1920 // example, it is not possible to goto into a stmt expression apparently.
1921 // More semantic analysis is needed.
1922
1923 // FIXME: the last statement in the compount stmt has its value used. We
1924 // should not warn about it being unused.
1925
1926 // If there are sub stmts in the compound stmt, take the type of the last one
1927 // as the type of the stmtexpr.
1928 QualType Ty = Context.VoidTy;
1929
1930 if (!Compound->body_empty())
1931 if (Expr *LastExpr = dyn_cast<Expr>(Compound->body_back()))
1932 Ty = LastExpr->getType();
1933
1934 return new StmtExpr(Compound, Ty, LPLoc, RPLoc);
1935}
Steve Naroff78864672007-08-01 22:05:33 +00001936
Steve Naroff66356bd2007-09-16 14:56:35 +00001937Sema::ExprResult Sema::ActOnBuiltinOffsetOf(SourceLocation BuiltinLoc,
Chris Lattnerf17bd422007-08-30 17:45:32 +00001938 SourceLocation TypeLoc,
1939 TypeTy *argty,
1940 OffsetOfComponent *CompPtr,
1941 unsigned NumComponents,
1942 SourceLocation RPLoc) {
1943 QualType ArgTy = QualType::getFromOpaquePtr(argty);
1944 assert(!ArgTy.isNull() && "Missing type argument!");
1945
1946 // We must have at least one component that refers to the type, and the first
1947 // one is known to be a field designator. Verify that the ArgTy represents
1948 // a struct/union/class.
1949 if (!ArgTy->isRecordType())
1950 return Diag(TypeLoc, diag::err_offsetof_record_type,ArgTy.getAsString());
1951
1952 // Otherwise, create a compound literal expression as the base, and
1953 // iteratively process the offsetof designators.
1954 Expr *Res = new CompoundLiteralExpr(ArgTy, 0);
1955
Chris Lattner78502cf2007-08-31 21:49:13 +00001956 // offsetof with non-identifier designators (e.g. "offsetof(x, a.b[c])") are a
1957 // GCC extension, diagnose them.
1958 if (NumComponents != 1)
1959 Diag(BuiltinLoc, diag::ext_offsetof_extended_field_designator,
1960 SourceRange(CompPtr[1].LocStart, CompPtr[NumComponents-1].LocEnd));
1961
Chris Lattnerf17bd422007-08-30 17:45:32 +00001962 for (unsigned i = 0; i != NumComponents; ++i) {
1963 const OffsetOfComponent &OC = CompPtr[i];
1964 if (OC.isBrackets) {
1965 // Offset of an array sub-field. TODO: Should we allow vector elements?
1966 const ArrayType *AT = Res->getType()->getAsArrayType();
1967 if (!AT) {
1968 delete Res;
1969 return Diag(OC.LocEnd, diag::err_offsetof_array_type,
1970 Res->getType().getAsString());
1971 }
1972
Chris Lattner98dbf0a2007-08-30 17:59:59 +00001973 // FIXME: C++: Verify that operator[] isn't overloaded.
1974
Chris Lattnerf17bd422007-08-30 17:45:32 +00001975 // C99 6.5.2.1p1
1976 Expr *Idx = static_cast<Expr*>(OC.U.E);
1977 if (!Idx->getType()->isIntegerType())
1978 return Diag(Idx->getLocStart(), diag::err_typecheck_subscript,
1979 Idx->getSourceRange());
1980
1981 Res = new ArraySubscriptExpr(Res, Idx, AT->getElementType(), OC.LocEnd);
1982 continue;
1983 }
1984
1985 const RecordType *RC = Res->getType()->getAsRecordType();
1986 if (!RC) {
1987 delete Res;
1988 return Diag(OC.LocEnd, diag::err_offsetof_record_type,
1989 Res->getType().getAsString());
1990 }
1991
1992 // Get the decl corresponding to this.
1993 RecordDecl *RD = RC->getDecl();
1994 FieldDecl *MemberDecl = RD->getMember(OC.U.IdentInfo);
1995 if (!MemberDecl)
1996 return Diag(BuiltinLoc, diag::err_typecheck_no_member,
1997 OC.U.IdentInfo->getName(),
1998 SourceRange(OC.LocStart, OC.LocEnd));
Chris Lattner98dbf0a2007-08-30 17:59:59 +00001999
2000 // FIXME: C++: Verify that MemberDecl isn't a static field.
2001 // FIXME: Verify that MemberDecl isn't a bitfield.
2002
Chris Lattnerf17bd422007-08-30 17:45:32 +00002003 Res = new MemberExpr(Res, false, MemberDecl, OC.LocEnd);
2004 }
2005
2006 return new UnaryOperator(Res, UnaryOperator::OffsetOf, Context.getSizeType(),
2007 BuiltinLoc);
2008}
2009
2010
Steve Naroff66356bd2007-09-16 14:56:35 +00002011Sema::ExprResult Sema::ActOnTypesCompatibleExpr(SourceLocation BuiltinLoc,
Steve Naroff78864672007-08-01 22:05:33 +00002012 TypeTy *arg1, TypeTy *arg2,
2013 SourceLocation RPLoc) {
2014 QualType argT1 = QualType::getFromOpaquePtr(arg1);
2015 QualType argT2 = QualType::getFromOpaquePtr(arg2);
2016
2017 assert((!argT1.isNull() && !argT2.isNull()) && "Missing type argument(s)");
2018
Chris Lattnerf17bd422007-08-30 17:45:32 +00002019 return new TypesCompatibleExpr(Context.IntTy, BuiltinLoc, argT1, argT2,RPLoc);
Steve Naroff78864672007-08-01 22:05:33 +00002020}
2021
Steve Naroff66356bd2007-09-16 14:56:35 +00002022Sema::ExprResult Sema::ActOnChooseExpr(SourceLocation BuiltinLoc, ExprTy *cond,
Steve Naroff9efdabc2007-08-03 21:21:27 +00002023 ExprTy *expr1, ExprTy *expr2,
2024 SourceLocation RPLoc) {
2025 Expr *CondExpr = static_cast<Expr*>(cond);
2026 Expr *LHSExpr = static_cast<Expr*>(expr1);
2027 Expr *RHSExpr = static_cast<Expr*>(expr2);
2028
2029 assert((CondExpr && LHSExpr && RHSExpr) && "Missing type argument(s)");
2030
2031 // The conditional expression is required to be a constant expression.
2032 llvm::APSInt condEval(32);
2033 SourceLocation ExpLoc;
2034 if (!CondExpr->isIntegerConstantExpr(condEval, Context, &ExpLoc))
2035 return Diag(ExpLoc, diag::err_typecheck_choose_expr_requires_constant,
2036 CondExpr->getSourceRange());
2037
2038 // If the condition is > zero, then the AST type is the same as the LSHExpr.
2039 QualType resType = condEval.getZExtValue() ? LHSExpr->getType() :
2040 RHSExpr->getType();
2041 return new ChooseExpr(BuiltinLoc, CondExpr, LHSExpr, RHSExpr, resType, RPLoc);
2042}
2043
Anders Carlsson7e13ab82007-10-15 20:28:48 +00002044Sema::ExprResult Sema::ActOnVAArg(SourceLocation BuiltinLoc,
2045 ExprTy *expr, TypeTy *type,
2046 SourceLocation RPLoc)
2047{
2048 Expr *E = static_cast<Expr*>(expr);
2049 QualType T = QualType::getFromOpaquePtr(type);
2050
2051 InitBuiltinVaListType();
2052
2053 Sema::AssignmentCheckResult result;
2054
2055 result = CheckAssignmentConstraints(Context.getBuiltinVaListType(),
2056 E->getType());
2057 if (result != Compatible)
2058 return Diag(E->getLocStart(),
2059 diag::err_first_argument_to_va_arg_not_of_type_va_list,
2060 E->getType().getAsString(),
2061 E->getSourceRange());
2062
2063 // FIXME: Warn if a non-POD type is passed in.
2064
2065 return new VAArgExpr(BuiltinLoc, E, T, RPLoc);
2066}
2067
Anders Carlsson76f4a902007-08-21 17:43:55 +00002068// TODO: Move this to SemaObjC.cpp
Chris Lattnere002fbe2007-12-12 01:04:12 +00002069Sema::ExprResult Sema::ParseObjCStringLiteral(SourceLocation *AtLocs,
2070 ExprTy **Strings,
2071 unsigned NumStrings) {
Chris Lattnere002fbe2007-12-12 01:04:12 +00002072 SourceLocation AtLoc = AtLocs[0];
2073 StringLiteral* S = static_cast<StringLiteral *>(Strings[0]);
Fariborz Jahanian4d1288a2007-12-12 23:55:49 +00002074 if (NumStrings > 1) {
2075 // Concatenate objc strings.
2076 StringLiteral* ES = static_cast<StringLiteral *>(Strings[NumStrings-1]);
2077 SourceLocation EndLoc = ES->getSourceRange().getEnd();
2078 unsigned Length = 0;
2079 for (unsigned i = 0; i < NumStrings; i++)
2080 Length += static_cast<StringLiteral *>(Strings[i])->getByteLength();
2081 char *strBuf = new char [Length];
2082 char *p = strBuf;
2083 bool isWide = false;
2084 for (unsigned i = 0; i < NumStrings; i++) {
2085 S = static_cast<StringLiteral *>(Strings[i]);
2086 if (S->isWide())
2087 isWide = true;
2088 memcpy(p, S->getStrData(), S->getByteLength());
2089 p += S->getByteLength();
2090 delete S;
2091 }
2092 S = new StringLiteral(strBuf, Length,
2093 isWide, Context.getPointerType(Context.CharTy),
2094 AtLoc, EndLoc);
2095 }
Anders Carlsson76f4a902007-08-21 17:43:55 +00002096
2097 if (CheckBuiltinCFStringArgument(S))
2098 return true;
2099
Steve Narofff73b7842007-10-15 23:35:17 +00002100 if (Context.getObjcConstantStringInterface().isNull()) {
2101 // Initialize the constant string interface lazily. This assumes
2102 // the NSConstantString interface is seen in this translation unit.
2103 IdentifierInfo *NSIdent = &Context.Idents.get("NSConstantString");
2104 ScopedDecl *IFace = LookupScopedDecl(NSIdent, Decl::IDNS_Ordinary,
2105 SourceLocation(), TUScope);
Steve Naroff69849552007-10-16 00:00:18 +00002106 ObjcInterfaceDecl *strIFace = dyn_cast_or_null<ObjcInterfaceDecl>(IFace);
Steve Naroff684dc412007-10-18 23:53:51 +00002107 if (!strIFace)
2108 return Diag(S->getLocStart(), diag::err_undef_interface,
2109 NSIdent->getName());
Steve Naroff69849552007-10-16 00:00:18 +00002110 Context.setObjcConstantStringInterface(strIFace);
Steve Narofff73b7842007-10-15 23:35:17 +00002111 }
2112 QualType t = Context.getObjcConstantStringInterface();
Anders Carlsson76f4a902007-08-21 17:43:55 +00002113 t = Context.getPointerType(t);
Steve Naroffa397efd2007-11-03 11:27:19 +00002114 return new ObjCStringLiteral(S, t, AtLoc);
Anders Carlsson76f4a902007-08-21 17:43:55 +00002115}
Anders Carlssonc5a81eb2007-08-22 15:14:15 +00002116
2117Sema::ExprResult Sema::ParseObjCEncodeExpression(SourceLocation AtLoc,
Chris Lattner37390be2007-10-16 22:51:17 +00002118 SourceLocation EncodeLoc,
Anders Carlssonc5a81eb2007-08-22 15:14:15 +00002119 SourceLocation LParenLoc,
2120 TypeTy *Ty,
2121 SourceLocation RParenLoc) {
2122 QualType EncodedType = QualType::getFromOpaquePtr(Ty);
2123
2124 QualType t = Context.getPointerType(Context.CharTy);
2125 return new ObjCEncodeExpr(t, EncodedType, AtLoc, RParenLoc);
2126}
Steve Narofff0f2afc2007-09-17 21:01:15 +00002127
Fariborz Jahanian4bef4622007-10-16 20:40:23 +00002128Sema::ExprResult Sema::ParseObjCSelectorExpression(Selector Sel,
2129 SourceLocation AtLoc,
Fariborz Jahanian6bd1d612007-10-16 23:21:02 +00002130 SourceLocation SelLoc,
Fariborz Jahanian4bef4622007-10-16 20:40:23 +00002131 SourceLocation LParenLoc,
2132 SourceLocation RParenLoc) {
Steve Naroff6d40db02007-10-31 18:42:27 +00002133 QualType t = Context.getObjcSelType();
Fariborz Jahanian4bef4622007-10-16 20:40:23 +00002134 return new ObjCSelectorExpr(t, Sel, AtLoc, RParenLoc);
2135}
2136
Fariborz Jahaniana32aaef2007-10-17 16:58:11 +00002137Sema::ExprResult Sema::ParseObjCProtocolExpression(IdentifierInfo *ProtocolId,
2138 SourceLocation AtLoc,
2139 SourceLocation ProtoLoc,
2140 SourceLocation LParenLoc,
2141 SourceLocation RParenLoc) {
2142 ObjcProtocolDecl* PDecl = ObjcProtocols[ProtocolId];
2143 if (!PDecl) {
2144 Diag(ProtoLoc, diag::err_undeclared_protocol, ProtocolId->getName());
2145 return true;
2146 }
2147
Fariborz Jahanian2bbd03a2007-12-07 00:18:54 +00002148 QualType t = Context.getObjcProtoType();
Fariborz Jahaniane183a822007-10-18 22:59:23 +00002149 if (t.isNull())
2150 return true;
Fariborz Jahanian2bbd03a2007-12-07 00:18:54 +00002151 t = Context.getPointerType(t);
Fariborz Jahaniana32aaef2007-10-17 16:58:11 +00002152 return new ObjCProtocolExpr(t, PDecl, AtLoc, RParenLoc);
2153}
Steve Naroff077c83b2007-10-16 23:12:48 +00002154
2155bool Sema::CheckMessageArgumentTypes(Expr **Args, unsigned NumArgs,
2156 ObjcMethodDecl *Method) {
2157 bool anyIncompatibleArgs = false;
2158
2159 for (unsigned i = 0; i < NumArgs; i++) {
2160 Expr *argExpr = Args[i];
2161 assert(argExpr && "CheckMessageArgumentTypes(): missing expression");
2162
2163 QualType lhsType = Method->getParamDecl(i)->getType();
2164 QualType rhsType = argExpr->getType();
2165
2166 // If necessary, apply function/array conversion. C99 6.7.5.3p[7,8].
2167 if (const ArrayType *ary = lhsType->getAsArrayType())
2168 lhsType = Context.getPointerType(ary->getElementType());
2169 else if (lhsType->isFunctionType())
2170 lhsType = Context.getPointerType(lhsType);
2171
2172 AssignmentCheckResult result = CheckSingleAssignmentConstraints(lhsType,
2173 argExpr);
2174 if (Args[i] != argExpr) // The expression was converted.
2175 Args[i] = argExpr; // Make sure we store the converted expression.
2176 SourceLocation l = argExpr->getLocStart();
2177
2178 // decode the result (notice that AST's are still created for extensions).
2179 switch (result) {
2180 case Compatible:
2181 break;
2182 case PointerFromInt:
Steve Naroff0ee0b0a2007-11-27 17:58:44 +00002183 Diag(l, diag::ext_typecheck_sending_pointer_int,
2184 lhsType.getAsString(), rhsType.getAsString(),
2185 argExpr->getSourceRange());
Steve Naroff077c83b2007-10-16 23:12:48 +00002186 break;
2187 case IntFromPointer:
2188 Diag(l, diag::ext_typecheck_sending_pointer_int,
2189 lhsType.getAsString(), rhsType.getAsString(),
2190 argExpr->getSourceRange());
2191 break;
2192 case IncompatiblePointer:
2193 Diag(l, diag::ext_typecheck_sending_incompatible_pointer,
2194 rhsType.getAsString(), lhsType.getAsString(),
2195 argExpr->getSourceRange());
2196 break;
2197 case CompatiblePointerDiscardsQualifiers:
2198 Diag(l, diag::ext_typecheck_passing_discards_qualifiers,
2199 rhsType.getAsString(), lhsType.getAsString(),
2200 argExpr->getSourceRange());
2201 break;
2202 case Incompatible:
2203 Diag(l, diag::err_typecheck_sending_incompatible,
2204 rhsType.getAsString(), lhsType.getAsString(),
2205 argExpr->getSourceRange());
2206 anyIncompatibleArgs = true;
2207 }
2208 }
2209 return anyIncompatibleArgs;
2210}
2211
Steve Narofff73590d2007-09-27 14:38:14 +00002212// ActOnClassMessage - used for both unary and keyword messages.
2213// ArgExprs is optional - if it is present, the number of expressions
2214// is obtained from Sel.getNumArgs().
2215Sema::ExprResult Sema::ActOnClassMessage(
Fariborz Jahaniand98a7342007-11-12 20:13:27 +00002216 Scope *S,
Steve Naroffc6814ea2007-10-02 20:01:56 +00002217 IdentifierInfo *receiverName, Selector Sel,
Steve Naroffe3ffc2f2007-11-15 13:05:42 +00002218 SourceLocation lbrac, SourceLocation rbrac, ExprTy **Args, unsigned NumArgs)
Steve Narofff0f2afc2007-09-17 21:01:15 +00002219{
Steve Naroffc6814ea2007-10-02 20:01:56 +00002220 assert(receiverName && "missing receiver class name");
Steve Naroffd54978b2007-09-18 23:55:05 +00002221
Steve Naroff077c83b2007-10-16 23:12:48 +00002222 Expr **ArgExprs = reinterpret_cast<Expr **>(Args);
Fariborz Jahaniand98a7342007-11-12 20:13:27 +00002223 ObjcInterfaceDecl* ClassDecl = 0;
2224 if (!strcmp(receiverName->getName(), "super") && CurMethodDecl) {
2225 ClassDecl = CurMethodDecl->getClassInterface()->getSuperClass();
Fariborz Jahanian2d0d3ba2007-11-12 20:20:37 +00002226 if (ClassDecl && CurMethodDecl->isInstance()) {
Steve Naroffb2f8ff12007-12-07 03:50:46 +00002227 // Synthesize a cast to the super class. This hack allows us to loosely
2228 // represent super without creating a special expression node.
Fariborz Jahaniand98a7342007-11-12 20:13:27 +00002229 IdentifierInfo &II = Context.Idents.get("self");
Steve Naroffb2f8ff12007-12-07 03:50:46 +00002230 ExprResult ReceiverExpr = ActOnIdentifierExpr(S, lbrac, II, false);
Fariborz Jahaniand98a7342007-11-12 20:13:27 +00002231 QualType superTy = Context.getObjcInterfaceType(ClassDecl);
2232 superTy = Context.getPointerType(superTy);
2233 ReceiverExpr = ActOnCastExpr(SourceLocation(), superTy.getAsOpaquePtr(),
2234 SourceLocation(), ReceiverExpr.Val);
Steve Naroffb2f8ff12007-12-07 03:50:46 +00002235 // We are really in an instance method, redirect.
Fariborz Jahaniand98a7342007-11-12 20:13:27 +00002236 return ActOnInstanceMessage(ReceiverExpr.Val, Sel, lbrac, rbrac,
Steve Naroffe3ffc2f2007-11-15 13:05:42 +00002237 Args, NumArgs);
Fariborz Jahaniand98a7342007-11-12 20:13:27 +00002238 }
Steve Naroffb2f8ff12007-12-07 03:50:46 +00002239 // We are sending a message to 'super' within a class method. Do nothing,
2240 // the receiver will pass through as 'super' (how convenient:-).
2241 } else
Fariborz Jahaniand98a7342007-11-12 20:13:27 +00002242 ClassDecl = getObjCInterfaceDecl(receiverName);
Steve Naroffb2f8ff12007-12-07 03:50:46 +00002243
2244 // FIXME: can ClassDecl ever be null?
Steve Naroffc6814ea2007-10-02 20:01:56 +00002245 ObjcMethodDecl *Method = ClassDecl->lookupClassMethod(Sel);
Steve Naroff55f52da2007-10-16 20:39:36 +00002246 QualType returnType;
Steve Naroff96d20612007-11-05 15:27:52 +00002247
2248 // Before we give up, check if the selector is an instance method.
2249 if (!Method)
2250 Method = ClassDecl->lookupInstanceMethod(Sel);
Steve Naroff55f52da2007-10-16 20:39:36 +00002251 if (!Method) {
2252 Diag(lbrac, diag::warn_method_not_found, std::string("+"), Sel.getName(),
2253 SourceRange(lbrac, rbrac));
Steve Naroff6d40db02007-10-31 18:42:27 +00002254 returnType = Context.getObjcIdType();
Steve Naroff55f52da2007-10-16 20:39:36 +00002255 } else {
Steve Naroffd2754262007-10-16 21:36:54 +00002256 returnType = Method->getResultType();
Steve Naroff077c83b2007-10-16 23:12:48 +00002257 if (Sel.getNumArgs()) {
2258 if (CheckMessageArgumentTypes(ArgExprs, Sel.getNumArgs(), Method))
2259 return true;
2260 }
Steve Naroff55f52da2007-10-16 20:39:36 +00002261 }
Steve Naroff66697aa2007-11-03 16:37:59 +00002262 return new ObjCMessageExpr(receiverName, Sel, returnType, Method,
Steve Naroffe3ffc2f2007-11-15 13:05:42 +00002263 lbrac, rbrac, ArgExprs, NumArgs);
Steve Narofff0f2afc2007-09-17 21:01:15 +00002264}
2265
Steve Narofff73590d2007-09-27 14:38:14 +00002266// ActOnInstanceMessage - used for both unary and keyword messages.
2267// ArgExprs is optional - if it is present, the number of expressions
2268// is obtained from Sel.getNumArgs().
2269Sema::ExprResult Sema::ActOnInstanceMessage(
Steve Naroff80175062007-09-28 22:22:11 +00002270 ExprTy *receiver, Selector Sel,
Steve Naroffe3ffc2f2007-11-15 13:05:42 +00002271 SourceLocation lbrac, SourceLocation rbrac, ExprTy **Args, unsigned NumArgs)
Steve Narofff73590d2007-09-27 14:38:14 +00002272{
Steve Naroffd54978b2007-09-18 23:55:05 +00002273 assert(receiver && "missing receiver expression");
2274
Steve Naroff077c83b2007-10-16 23:12:48 +00002275 Expr **ArgExprs = reinterpret_cast<Expr **>(Args);
Steve Naroffd54978b2007-09-18 23:55:05 +00002276 Expr *RExpr = static_cast<Expr *>(receiver);
Steve Naroffc6814ea2007-10-02 20:01:56 +00002277 QualType receiverType = RExpr->getType();
Steve Naroff7f549f12007-10-10 21:53:07 +00002278 QualType returnType;
Steve Naroff66697aa2007-11-03 16:37:59 +00002279 ObjcMethodDecl *Method;
Steve Naroff7f549f12007-10-10 21:53:07 +00002280
Steve Naroff49effde2007-11-11 17:52:25 +00002281 if (receiverType == Context.getObjcIdType() ||
2282 receiverType == Context.getObjcClassType()) {
Steve Naroff66697aa2007-11-03 16:37:59 +00002283 Method = InstanceMethodPool[Sel].Method;
Steve Naroff1d2538c2007-12-18 01:30:32 +00002284 if (!Method)
2285 Method = FactoryMethodPool[Sel].Method;
Steve Naroff55f52da2007-10-16 20:39:36 +00002286 if (!Method) {
2287 Diag(lbrac, diag::warn_method_not_found, std::string("-"), Sel.getName(),
2288 SourceRange(lbrac, rbrac));
Steve Naroff6d40db02007-10-31 18:42:27 +00002289 returnType = Context.getObjcIdType();
Steve Naroff55f52da2007-10-16 20:39:36 +00002290 } else {
Steve Naroffd2754262007-10-16 21:36:54 +00002291 returnType = Method->getResultType();
Steve Naroff077c83b2007-10-16 23:12:48 +00002292 if (Sel.getNumArgs())
2293 if (CheckMessageArgumentTypes(ArgExprs, Sel.getNumArgs(), Method))
2294 return true;
Steve Naroff55f52da2007-10-16 20:39:36 +00002295 }
Steve Naroff7f549f12007-10-10 21:53:07 +00002296 } else {
Fariborz Jahanian24cb52c2007-12-17 21:03:50 +00002297 bool receiverIsQualId =
2298 dyn_cast<ObjcQualifiedIdType>(RExpr->getType()) != 0;
Chris Lattner9c7a0362007-10-10 23:42:28 +00002299 // FIXME (snaroff): checking in this code from Patrick. Needs to be
2300 // revisited. how do we get the ClassDecl from the receiver expression?
Fariborz Jahanian24cb52c2007-12-17 21:03:50 +00002301 if (!receiverIsQualId)
2302 while (receiverType->isPointerType()) {
2303 PointerType *pointerType =
2304 static_cast<PointerType*>(receiverType.getTypePtr());
2305 receiverType = pointerType->getPointeeType();
2306 }
Fariborz Jahanianff6a4552007-12-07 21:21:21 +00002307 ObjcInterfaceDecl* ClassDecl;
2308 if (ObjcQualifiedInterfaceType *QIT =
2309 dyn_cast<ObjcQualifiedInterfaceType>(receiverType)) {
Fariborz Jahanianc47dc4f2007-12-13 20:47:42 +00002310 ClassDecl = QIT->getDecl();
Fariborz Jahanianff6a4552007-12-07 21:21:21 +00002311 Method = ClassDecl->lookupInstanceMethod(Sel);
2312 if (!Method) {
2313 // search protocols
2314 for (unsigned i = 0; i < QIT->getNumProtocols(); i++) {
2315 ObjcProtocolDecl *PDecl = QIT->getProtocols(i);
2316 if (PDecl && (Method = PDecl->lookupInstanceMethod(Sel)))
2317 break;
2318 }
2319 }
Fariborz Jahanian24cb52c2007-12-17 21:03:50 +00002320 if (!Method)
2321 Diag(lbrac, diag::warn_method_not_found_in_protocol,
2322 std::string("-"), Sel.getName(),
2323 SourceRange(lbrac, rbrac));
2324 }
2325 else if (ObjcQualifiedIdType *QIT =
2326 dyn_cast<ObjcQualifiedIdType>(receiverType)) {
2327 // search protocols
2328 for (unsigned i = 0; i < QIT->getNumProtocols(); i++) {
2329 ObjcProtocolDecl *PDecl = QIT->getProtocols(i);
2330 if (PDecl && (Method = PDecl->lookupInstanceMethod(Sel)))
2331 break;
2332 }
2333 if (!Method)
2334 Diag(lbrac, diag::warn_method_not_found_in_protocol,
2335 std::string("-"), Sel.getName(),
2336 SourceRange(lbrac, rbrac));
Fariborz Jahanianff6a4552007-12-07 21:21:21 +00002337 }
2338 else {
2339 assert(ObjcInterfaceType::classof(receiverType.getTypePtr()) &&
2340 "bad receiver type");
2341 ClassDecl = static_cast<ObjcInterfaceType*>(
2342 receiverType.getTypePtr())->getDecl();
2343 // FIXME: consider using InstanceMethodPool, since it will be faster
2344 // than the following method (which can do *many* linear searches). The
2345 // idea is to add class info to InstanceMethodPool...
2346 Method = ClassDecl->lookupInstanceMethod(Sel);
2347 }
Steve Naroff55f52da2007-10-16 20:39:36 +00002348 if (!Method) {
Steve Naroff22e078e2007-11-11 00:10:47 +00002349 // If we have an implementation in scope, check "private" methods.
2350 if (ObjcImplementationDecl *ImpDecl =
2351 ObjcImplementations[ClassDecl->getIdentifier()])
Steve Narofffa5751f2007-12-19 22:27:04 +00002352 Method = ImpDecl->getInstanceMethod(Sel);
Steve Naroff5cace622007-12-11 03:38:03 +00002353 // If we still haven't found a method, look in the global pool. This
2354 // behavior isn't very desirable, however we need it for GCC compatibility.
Steve Naroff888f33c2007-12-07 20:41:14 +00002355 if (!Method)
2356 Method = InstanceMethodPool[Sel].Method;
Steve Naroff22e078e2007-11-11 00:10:47 +00002357 }
2358 if (!Method) {
Steve Naroff55f52da2007-10-16 20:39:36 +00002359 Diag(lbrac, diag::warn_method_not_found, std::string("-"), Sel.getName(),
2360 SourceRange(lbrac, rbrac));
Steve Naroff6d40db02007-10-31 18:42:27 +00002361 returnType = Context.getObjcIdType();
Steve Naroff55f52da2007-10-16 20:39:36 +00002362 } else {
Steve Naroffd2754262007-10-16 21:36:54 +00002363 returnType = Method->getResultType();
Steve Naroff077c83b2007-10-16 23:12:48 +00002364 if (Sel.getNumArgs())
2365 if (CheckMessageArgumentTypes(ArgExprs, Sel.getNumArgs(), Method))
2366 return true;
Steve Naroff55f52da2007-10-16 20:39:36 +00002367 }
Steve Naroffc6814ea2007-10-02 20:01:56 +00002368 }
Steve Naroff66697aa2007-11-03 16:37:59 +00002369 return new ObjCMessageExpr(RExpr, Sel, returnType, Method, lbrac, rbrac,
Steve Naroffe3ffc2f2007-11-15 13:05:42 +00002370 ArgExprs, NumArgs);
Steve Narofff0f2afc2007-09-17 21:01:15 +00002371}