blob: 12897ea291ccf7a0d8ef8a9b77cdf07e0f7b82b1 [file] [log] [blame]
Chris Lattner4b009652007-07-25 00:24:17 +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"
15#include "clang/AST/ASTContext.h"
16#include "clang/AST/Decl.h"
Steve Narofffa465d12007-10-02 20:01:56 +000017#include "clang/AST/DeclObjC.h"
Chris Lattner4b009652007-07-25 00:24:17 +000018#include "clang/AST/Expr.h"
Steve Naroffc39ca262007-09-18 23:55:05 +000019#include "clang/Parse/DeclSpec.h"
Chris Lattner4b009652007-07-25 00:24:17 +000020#include "clang/Lex/Preprocessor.h"
21#include "clang/Lex/LiteralSupport.h"
22#include "clang/Basic/SourceManager.h"
23#include "clang/Basic/Diagnostic.h"
24#include "clang/Basic/LangOptions.h"
25#include "clang/Basic/TargetInfo.h"
26#include "llvm/ADT/SmallString.h"
Chris Lattner2e64c072007-08-10 20:18:51 +000027#include "llvm/ADT/StringExtras.h"
Chris Lattner4b009652007-07-25 00:24:17 +000028using namespace clang;
29
Steve Naroff87d58b42007-09-16 03:34:24 +000030/// ActOnStringLiteral - The specified tokens were lexed as pasted string
Chris Lattner4b009652007-07-25 00:24:17 +000031/// fragments (e.g. "foo" "bar" L"baz"). The result string has to handle string
32/// concatenation ([C99 5.1.1.2, translation phase #6]), so it may come from
33/// multiple tokens. However, the common case is that StringToks points to one
34/// string.
35///
36Action::ExprResult
Steve Naroff87d58b42007-09-16 03:34:24 +000037Sema::ActOnStringLiteral(const Token *StringToks, unsigned NumStringToks) {
Chris Lattner4b009652007-07-25 00:24:17 +000038 assert(NumStringToks && "Must have at least one string!");
39
40 StringLiteralParser Literal(StringToks, NumStringToks, PP, Context.Target);
41 if (Literal.hadError)
42 return ExprResult(true);
43
44 llvm::SmallVector<SourceLocation, 4> StringTokLocs;
45 for (unsigned i = 0; i != NumStringToks; ++i)
46 StringTokLocs.push_back(StringToks[i].getLocation());
47
48 // FIXME: handle wchar_t
Anders Carlsson55bfe0d2007-10-15 02:50:23 +000049 QualType t;
50
51 if (Literal.Pascal)
52 t = Context.getPointerType(Context.UnsignedCharTy);
53 else
54 t = Context.getPointerType(Context.CharTy);
55
56 if (Literal.Pascal && Literal.GetStringLength() > 256)
57 return Diag(StringToks[0].getLocation(), diag::err_pascal_string_too_long,
58 SourceRange(StringToks[0].getLocation(),
59 StringToks[NumStringToks-1].getLocation()));
Chris Lattner4b009652007-07-25 00:24:17 +000060
61 // Pass &StringTokLocs[0], StringTokLocs.size() to factory!
62 return new StringLiteral(Literal.GetString(), Literal.GetStringLength(),
Anders Carlsson55bfe0d2007-10-15 02:50:23 +000063 Literal.AnyWide, t,
64 StringToks[0].getLocation(),
Chris Lattner4b009652007-07-25 00:24:17 +000065 StringToks[NumStringToks-1].getLocation());
66}
67
68
Steve Naroff0acc9c92007-09-15 18:49:24 +000069/// ActOnIdentifierExpr - The parser read an identifier in expression context,
Chris Lattner4b009652007-07-25 00:24:17 +000070/// validate it per-C99 6.5.1. HasTrailingLParen indicates whether this
71/// identifier is used in an function call context.
Steve Naroff0acc9c92007-09-15 18:49:24 +000072Sema::ExprResult Sema::ActOnIdentifierExpr(Scope *S, SourceLocation Loc,
Chris Lattner4b009652007-07-25 00:24:17 +000073 IdentifierInfo &II,
74 bool HasTrailingLParen) {
75 // Could be enum-constant or decl.
Steve Narofff0c31dd2007-09-16 16:16:00 +000076 ScopedDecl *D = LookupScopedDecl(&II, Decl::IDNS_Ordinary, Loc, S);
Chris Lattner4b009652007-07-25 00:24:17 +000077 if (D == 0) {
78 // Otherwise, this could be an implicitly declared function reference (legal
79 // in C90, extension in C99).
80 if (HasTrailingLParen &&
81 // Not in C++.
82 !getLangOptions().CPlusPlus)
83 D = ImplicitlyDefineFunction(Loc, II, S);
84 else {
85 // If this name wasn't predeclared and if this is not a function call,
86 // diagnose the problem.
87 return Diag(Loc, diag::err_undeclared_var_use, II.getName());
88 }
89 }
Steve Naroff91b03f72007-08-28 03:03:08 +000090 if (ValueDecl *VD = dyn_cast<ValueDecl>(D)) {
Steve Naroffcae537d2007-08-28 18:45:29 +000091 // Only create DeclRefExpr's for valid Decl's.
Steve Naroffd1ad6ae2007-08-28 20:14:24 +000092 if (VD->isInvalidDecl())
Steve Naroff91b03f72007-08-28 03:03:08 +000093 return true;
Chris Lattner4b009652007-07-25 00:24:17 +000094 return new DeclRefExpr(VD, VD->getType(), Loc);
Steve Naroff91b03f72007-08-28 03:03:08 +000095 }
Chris Lattner4b009652007-07-25 00:24:17 +000096 if (isa<TypedefDecl>(D))
97 return Diag(Loc, diag::err_unexpected_typedef, II.getName());
98
99 assert(0 && "Invalid decl");
100 abort();
101}
102
Steve Naroff87d58b42007-09-16 03:34:24 +0000103Sema::ExprResult Sema::ActOnPreDefinedExpr(SourceLocation Loc,
Chris Lattner4b009652007-07-25 00:24:17 +0000104 tok::TokenKind Kind) {
105 PreDefinedExpr::IdentType IT;
106
107 switch (Kind) {
108 default:
109 assert(0 && "Unknown simple primary expr!");
110 case tok::kw___func__: // primary-expression: __func__ [C99 6.4.2.2]
111 IT = PreDefinedExpr::Func;
112 break;
113 case tok::kw___FUNCTION__: // primary-expression: __FUNCTION__ [GNU]
114 IT = PreDefinedExpr::Function;
115 break;
116 case tok::kw___PRETTY_FUNCTION__: // primary-expression: __P..Y_F..N__ [GNU]
117 IT = PreDefinedExpr::PrettyFunction;
118 break;
119 }
120
121 // Pre-defined identifiers are always of type char *.
122 return new PreDefinedExpr(Loc, Context.getPointerType(Context.CharTy), IT);
123}
124
Steve Naroff87d58b42007-09-16 03:34:24 +0000125Sema::ExprResult Sema::ActOnCharacterConstant(const Token &Tok) {
Chris Lattner4b009652007-07-25 00:24:17 +0000126 llvm::SmallString<16> CharBuffer;
127 CharBuffer.resize(Tok.getLength());
128 const char *ThisTokBegin = &CharBuffer[0];
129 unsigned ActualLength = PP.getSpelling(Tok, ThisTokBegin);
130
131 CharLiteralParser Literal(ThisTokBegin, ThisTokBegin+ActualLength,
132 Tok.getLocation(), PP);
133 if (Literal.hadError())
134 return ExprResult(true);
135 return new CharacterLiteral(Literal.getValue(), Context.IntTy,
136 Tok.getLocation());
137}
138
Steve Naroff87d58b42007-09-16 03:34:24 +0000139Action::ExprResult Sema::ActOnNumericConstant(const Token &Tok) {
Chris Lattner4b009652007-07-25 00:24:17 +0000140 // fast path for a single digit (which is quite common). A single digit
141 // cannot have a trigraph, escaped newline, radix prefix, or type suffix.
142 if (Tok.getLength() == 1) {
143 const char *t = PP.getSourceManager().getCharacterData(Tok.getLocation());
144
Chris Lattner3496d522007-09-04 02:45:27 +0000145 unsigned IntSize = static_cast<unsigned>(
146 Context.getTypeSize(Context.IntTy, Tok.getLocation()));
Chris Lattner4b009652007-07-25 00:24:17 +0000147 return ExprResult(new IntegerLiteral(llvm::APInt(IntSize, *t-'0'),
148 Context.IntTy,
149 Tok.getLocation()));
150 }
151 llvm::SmallString<512> IntegerBuffer;
152 IntegerBuffer.resize(Tok.getLength());
153 const char *ThisTokBegin = &IntegerBuffer[0];
154
155 // Get the spelling of the token, which eliminates trigraphs, etc.
156 unsigned ActualLength = PP.getSpelling(Tok, ThisTokBegin);
157 NumericLiteralParser Literal(ThisTokBegin, ThisTokBegin+ActualLength,
158 Tok.getLocation(), PP);
159 if (Literal.hadError)
160 return ExprResult(true);
161
Chris Lattner1de66eb2007-08-26 03:42:43 +0000162 Expr *Res;
163
164 if (Literal.isFloatingLiteral()) {
Chris Lattner858eece2007-09-22 18:29:59 +0000165 QualType Ty;
166 const llvm::fltSemantics *Format;
167 uint64_t Size; unsigned Align;
168
169 if (Literal.isFloat) {
170 Ty = Context.FloatTy;
171 Context.Target.getFloatInfo(Size, Align, Format, Tok.getLocation());
172 } else if (Literal.isLong) {
173 Ty = Context.LongDoubleTy;
174 Context.Target.getLongDoubleInfo(Size, Align, Format, Tok.getLocation());
175 } else {
176 Ty = Context.DoubleTy;
177 Context.Target.getDoubleInfo(Size, Align, Format, Tok.getLocation());
178 }
179
180 Res = new FloatingLiteral(Literal.GetFloatValue(*Format), Ty,
181 Tok.getLocation());
Chris Lattner1de66eb2007-08-26 03:42:43 +0000182 } else if (!Literal.isIntegerLiteral()) {
183 return ExprResult(true);
184 } else {
Chris Lattner4b009652007-07-25 00:24:17 +0000185 QualType t;
186
Neil Booth7421e9c2007-08-29 22:00:19 +0000187 // long long is a C99 feature.
188 if (!getLangOptions().C99 && !getLangOptions().CPlusPlus0x &&
Neil Booth9bd47082007-08-29 22:13:52 +0000189 Literal.isLongLong)
Neil Booth7421e9c2007-08-29 22:00:19 +0000190 Diag(Tok.getLocation(), diag::ext_longlong);
191
Chris Lattner4b009652007-07-25 00:24:17 +0000192 // Get the value in the widest-possible width.
193 llvm::APInt ResultVal(Context.Target.getIntMaxTWidth(Tok.getLocation()), 0);
194
195 if (Literal.GetIntegerValue(ResultVal)) {
196 // If this value didn't fit into uintmax_t, warn and force to ull.
197 Diag(Tok.getLocation(), diag::warn_integer_too_large);
198 t = Context.UnsignedLongLongTy;
199 assert(Context.getTypeSize(t, Tok.getLocation()) ==
200 ResultVal.getBitWidth() && "long long is not intmax_t?");
201 } else {
202 // If this value fits into a ULL, try to figure out what else it fits into
203 // according to the rules of C99 6.4.4.1p5.
204
205 // Octal, Hexadecimal, and integers with a U suffix are allowed to
206 // be an unsigned int.
207 bool AllowUnsigned = Literal.isUnsigned || Literal.getRadix() != 10;
208
209 // Check from smallest to largest, picking the smallest type we can.
Chris Lattner98540b62007-08-23 21:58:08 +0000210 if (!Literal.isLong && !Literal.isLongLong) {
211 // Are int/unsigned possibilities?
Chris Lattner3496d522007-09-04 02:45:27 +0000212 unsigned IntSize = static_cast<unsigned>(
213 Context.getTypeSize(Context.IntTy,Tok.getLocation()));
Chris Lattner4b009652007-07-25 00:24:17 +0000214 // Does it fit in a unsigned int?
215 if (ResultVal.isIntN(IntSize)) {
216 // Does it fit in a signed int?
217 if (!Literal.isUnsigned && ResultVal[IntSize-1] == 0)
218 t = Context.IntTy;
219 else if (AllowUnsigned)
220 t = Context.UnsignedIntTy;
221 }
222
223 if (!t.isNull())
224 ResultVal.trunc(IntSize);
225 }
226
227 // Are long/unsigned long possibilities?
228 if (t.isNull() && !Literal.isLongLong) {
Chris Lattner3496d522007-09-04 02:45:27 +0000229 unsigned LongSize = static_cast<unsigned>(
230 Context.getTypeSize(Context.LongTy, Tok.getLocation()));
Chris Lattner4b009652007-07-25 00:24:17 +0000231
232 // Does it fit in a unsigned long?
233 if (ResultVal.isIntN(LongSize)) {
234 // Does it fit in a signed long?
235 if (!Literal.isUnsigned && ResultVal[LongSize-1] == 0)
236 t = Context.LongTy;
237 else if (AllowUnsigned)
238 t = Context.UnsignedLongTy;
239 }
240 if (!t.isNull())
241 ResultVal.trunc(LongSize);
242 }
243
244 // Finally, check long long if needed.
245 if (t.isNull()) {
Chris Lattner3496d522007-09-04 02:45:27 +0000246 unsigned LongLongSize = static_cast<unsigned>(
247 Context.getTypeSize(Context.LongLongTy, Tok.getLocation()));
Chris Lattner4b009652007-07-25 00:24:17 +0000248
249 // Does it fit in a unsigned long long?
250 if (ResultVal.isIntN(LongLongSize)) {
251 // Does it fit in a signed long long?
252 if (!Literal.isUnsigned && ResultVal[LongLongSize-1] == 0)
253 t = Context.LongLongTy;
254 else if (AllowUnsigned)
255 t = Context.UnsignedLongLongTy;
256 }
257 }
258
259 // If we still couldn't decide a type, we probably have something that
260 // does not fit in a signed long long, but has no U suffix.
261 if (t.isNull()) {
262 Diag(Tok.getLocation(), diag::warn_integer_too_large_for_signed);
263 t = Context.UnsignedLongLongTy;
264 }
265 }
266
Chris Lattner1de66eb2007-08-26 03:42:43 +0000267 Res = new IntegerLiteral(ResultVal, t, Tok.getLocation());
Chris Lattner4b009652007-07-25 00:24:17 +0000268 }
Chris Lattner1de66eb2007-08-26 03:42:43 +0000269
270 // If this is an imaginary literal, create the ImaginaryLiteral wrapper.
271 if (Literal.isImaginary)
272 Res = new ImaginaryLiteral(Res, Context.getComplexType(Res->getType()));
273
274 return Res;
Chris Lattner4b009652007-07-25 00:24:17 +0000275}
276
Steve Naroff87d58b42007-09-16 03:34:24 +0000277Action::ExprResult Sema::ActOnParenExpr(SourceLocation L, SourceLocation R,
Chris Lattner4b009652007-07-25 00:24:17 +0000278 ExprTy *Val) {
279 Expr *e = (Expr *)Val;
Steve Naroff87d58b42007-09-16 03:34:24 +0000280 assert((e != 0) && "ActOnParenExpr() missing expr");
Chris Lattner4b009652007-07-25 00:24:17 +0000281 return new ParenExpr(L, R, e);
282}
283
284/// The UsualUnaryConversions() function is *not* called by this routine.
285/// See C99 6.3.2.1p[2-4] for more details.
286QualType Sema::CheckSizeOfAlignOfOperand(QualType exprType,
287 SourceLocation OpLoc, bool isSizeof) {
288 // C99 6.5.3.4p1:
289 if (isa<FunctionType>(exprType) && isSizeof)
290 // alignof(function) is allowed.
291 Diag(OpLoc, diag::ext_sizeof_function_type);
292 else if (exprType->isVoidType())
293 Diag(OpLoc, diag::ext_sizeof_void_type, isSizeof ? "sizeof" : "__alignof");
294 else if (exprType->isIncompleteType()) {
295 Diag(OpLoc, isSizeof ? diag::err_sizeof_incomplete_type :
296 diag::err_alignof_incomplete_type,
297 exprType.getAsString());
298 return QualType(); // error
299 }
300 // C99 6.5.3.4p4: the type (an unsigned integer type) is size_t.
301 return Context.getSizeType();
302}
303
304Action::ExprResult Sema::
Steve Naroff87d58b42007-09-16 03:34:24 +0000305ActOnSizeOfAlignOfTypeExpr(SourceLocation OpLoc, bool isSizeof,
Chris Lattner4b009652007-07-25 00:24:17 +0000306 SourceLocation LPLoc, TypeTy *Ty,
307 SourceLocation RPLoc) {
308 // If error parsing type, ignore.
309 if (Ty == 0) return true;
310
311 // Verify that this is a valid expression.
312 QualType ArgTy = QualType::getFromOpaquePtr(Ty);
313
314 QualType resultType = CheckSizeOfAlignOfOperand(ArgTy, OpLoc, isSizeof);
315
316 if (resultType.isNull())
317 return true;
318 return new SizeOfAlignOfTypeExpr(isSizeof, ArgTy, resultType, OpLoc, RPLoc);
319}
320
Chris Lattner5110ad52007-08-24 21:41:10 +0000321QualType Sema::CheckRealImagOperand(Expr *&V, SourceLocation Loc) {
Chris Lattner03931a72007-08-24 21:16:53 +0000322 DefaultFunctionArrayConversion(V);
323
Chris Lattnera16e42d2007-08-26 05:39:26 +0000324 // These operators return the element type of a complex type.
Chris Lattner03931a72007-08-24 21:16:53 +0000325 if (const ComplexType *CT = V->getType()->getAsComplexType())
326 return CT->getElementType();
Chris Lattnera16e42d2007-08-26 05:39:26 +0000327
328 // Otherwise they pass through real integer and floating point types here.
329 if (V->getType()->isArithmeticType())
330 return V->getType();
331
332 // Reject anything else.
333 Diag(Loc, diag::err_realimag_invalid_type, V->getType().getAsString());
334 return QualType();
Chris Lattner03931a72007-08-24 21:16:53 +0000335}
336
337
Chris Lattner4b009652007-07-25 00:24:17 +0000338
Steve Naroff87d58b42007-09-16 03:34:24 +0000339Action::ExprResult Sema::ActOnPostfixUnaryOp(SourceLocation OpLoc,
Chris Lattner4b009652007-07-25 00:24:17 +0000340 tok::TokenKind Kind,
341 ExprTy *Input) {
342 UnaryOperator::Opcode Opc;
343 switch (Kind) {
344 default: assert(0 && "Unknown unary op!");
345 case tok::plusplus: Opc = UnaryOperator::PostInc; break;
346 case tok::minusminus: Opc = UnaryOperator::PostDec; break;
347 }
348 QualType result = CheckIncrementDecrementOperand((Expr *)Input, OpLoc);
349 if (result.isNull())
350 return true;
351 return new UnaryOperator((Expr *)Input, Opc, result, OpLoc);
352}
353
354Action::ExprResult Sema::
Steve Naroff87d58b42007-09-16 03:34:24 +0000355ActOnArraySubscriptExpr(ExprTy *Base, SourceLocation LLoc,
Chris Lattner4b009652007-07-25 00:24:17 +0000356 ExprTy *Idx, SourceLocation RLoc) {
357 Expr *LHSExp = static_cast<Expr*>(Base), *RHSExp = static_cast<Expr*>(Idx);
358
359 // Perform default conversions.
360 DefaultFunctionArrayConversion(LHSExp);
361 DefaultFunctionArrayConversion(RHSExp);
362
363 QualType LHSTy = LHSExp->getType(), RHSTy = RHSExp->getType();
364
365 // C99 6.5.2.1p2: the expression e1[e2] is by definition precisely equivalent
Chris Lattner0d9bcea2007-08-30 17:45:32 +0000366 // to the expression *((e1)+(e2)). This means the array "Base" may actually be
Chris Lattner4b009652007-07-25 00:24:17 +0000367 // in the subscript position. As a result, we need to derive the array base
368 // and index from the expression types.
369 Expr *BaseExpr, *IndexExpr;
370 QualType ResultType;
Chris Lattner7931f4a2007-07-31 16:53:04 +0000371 if (const PointerType *PTy = LHSTy->getAsPointerType()) {
Chris Lattner4b009652007-07-25 00:24:17 +0000372 BaseExpr = LHSExp;
373 IndexExpr = RHSExp;
374 // FIXME: need to deal with const...
375 ResultType = PTy->getPointeeType();
Chris Lattner7931f4a2007-07-31 16:53:04 +0000376 } else if (const PointerType *PTy = RHSTy->getAsPointerType()) {
Chris Lattner4b009652007-07-25 00:24:17 +0000377 // Handle the uncommon case of "123[Ptr]".
378 BaseExpr = RHSExp;
379 IndexExpr = LHSExp;
380 // FIXME: need to deal with const...
381 ResultType = PTy->getPointeeType();
Chris Lattnere35a1042007-07-31 19:29:30 +0000382 } else if (const VectorType *VTy = LHSTy->getAsVectorType()) {
383 BaseExpr = LHSExp; // vectors: V[123]
Chris Lattner4b009652007-07-25 00:24:17 +0000384 IndexExpr = RHSExp;
Steve Naroff89345522007-08-03 22:40:33 +0000385
386 // Component access limited to variables (reject vec4.rg[1]).
387 if (!isa<DeclRefExpr>(BaseExpr))
388 return Diag(LLoc, diag::err_ocuvector_component_access,
389 SourceRange(LLoc, RLoc));
Chris Lattner4b009652007-07-25 00:24:17 +0000390 // FIXME: need to deal with const...
391 ResultType = VTy->getElementType();
392 } else {
393 return Diag(LHSExp->getLocStart(), diag::err_typecheck_subscript_value,
394 RHSExp->getSourceRange());
395 }
396 // C99 6.5.2.1p1
397 if (!IndexExpr->getType()->isIntegerType())
398 return Diag(IndexExpr->getLocStart(), diag::err_typecheck_subscript,
399 IndexExpr->getSourceRange());
400
401 // C99 6.5.2.1p1: "shall have type "pointer to *object* type". In practice,
402 // the following check catches trying to index a pointer to a function (e.g.
403 // void (*)(int)). Functions are not objects in C99.
404 if (!ResultType->isObjectType())
405 return Diag(BaseExpr->getLocStart(),
406 diag::err_typecheck_subscript_not_object,
407 BaseExpr->getType().getAsString(), BaseExpr->getSourceRange());
408
409 return new ArraySubscriptExpr(LHSExp, RHSExp, ResultType, RLoc);
410}
411
Steve Naroff1b8a46c2007-07-27 22:15:19 +0000412QualType Sema::
413CheckOCUVectorComponent(QualType baseType, SourceLocation OpLoc,
414 IdentifierInfo &CompName, SourceLocation CompLoc) {
Chris Lattnere35a1042007-07-31 19:29:30 +0000415 const OCUVectorType *vecType = baseType->getAsOCUVectorType();
Steve Naroff1b8a46c2007-07-27 22:15:19 +0000416
417 // The vector accessor can't exceed the number of elements.
418 const char *compStr = CompName.getName();
419 if (strlen(compStr) > vecType->getNumElements()) {
420 Diag(OpLoc, diag::err_ocuvector_component_exceeds_length,
421 baseType.getAsString(), SourceRange(CompLoc));
422 return QualType();
423 }
424 // The component names must come from the same set.
Chris Lattner9096b792007-08-02 22:33:49 +0000425 if (vecType->getPointAccessorIdx(*compStr) != -1) {
426 do
427 compStr++;
428 while (*compStr && vecType->getPointAccessorIdx(*compStr) != -1);
429 } else if (vecType->getColorAccessorIdx(*compStr) != -1) {
430 do
431 compStr++;
432 while (*compStr && vecType->getColorAccessorIdx(*compStr) != -1);
433 } else if (vecType->getTextureAccessorIdx(*compStr) != -1) {
434 do
435 compStr++;
436 while (*compStr && vecType->getTextureAccessorIdx(*compStr) != -1);
437 }
Steve Naroff1b8a46c2007-07-27 22:15:19 +0000438
439 if (*compStr) {
440 // We didn't get to the end of the string. This means the component names
441 // didn't come from the same set *or* we encountered an illegal name.
442 Diag(OpLoc, diag::err_ocuvector_component_name_illegal,
443 std::string(compStr,compStr+1), SourceRange(CompLoc));
444 return QualType();
445 }
446 // Each component accessor can't exceed the vector type.
447 compStr = CompName.getName();
448 while (*compStr) {
449 if (vecType->isAccessorWithinNumElements(*compStr))
450 compStr++;
451 else
452 break;
453 }
454 if (*compStr) {
455 // We didn't get to the end of the string. This means a component accessor
456 // exceeds the number of elements in the vector.
457 Diag(OpLoc, diag::err_ocuvector_component_exceeds_length,
458 baseType.getAsString(), SourceRange(CompLoc));
459 return QualType();
460 }
461 // The component accessor looks fine - now we need to compute the actual type.
462 // The vector type is implied by the component accessor. For example,
463 // vec4.b is a float, vec4.xy is a vec2, vec4.rgb is a vec3, etc.
464 unsigned CompSize = strlen(CompName.getName());
465 if (CompSize == 1)
466 return vecType->getElementType();
Steve Naroff82113e32007-07-29 16:33:31 +0000467
468 QualType VT = Context.getOCUVectorType(vecType->getElementType(), CompSize);
469 // Now look up the TypeDefDecl from the vector type. Without this,
470 // diagostics look bad. We want OCU vector types to appear built-in.
471 for (unsigned i = 0, e = OCUVectorDecls.size(); i != e; ++i) {
472 if (OCUVectorDecls[i]->getUnderlyingType() == VT)
473 return Context.getTypedefType(OCUVectorDecls[i]);
474 }
475 return VT; // should never get here (a typedef type should always be found).
Steve Naroff1b8a46c2007-07-27 22:15:19 +0000476}
477
Chris Lattner4b009652007-07-25 00:24:17 +0000478Action::ExprResult Sema::
Steve Naroff87d58b42007-09-16 03:34:24 +0000479ActOnMemberReferenceExpr(ExprTy *Base, SourceLocation OpLoc,
Chris Lattner4b009652007-07-25 00:24:17 +0000480 tok::TokenKind OpKind, SourceLocation MemberLoc,
481 IdentifierInfo &Member) {
Steve Naroff2cb66382007-07-26 03:11:44 +0000482 Expr *BaseExpr = static_cast<Expr *>(Base);
483 assert(BaseExpr && "no record expression");
Chris Lattner4b009652007-07-25 00:24:17 +0000484
Steve Naroff2cb66382007-07-26 03:11:44 +0000485 QualType BaseType = BaseExpr->getType();
486 assert(!BaseType.isNull() && "no type for member expression");
Chris Lattner4b009652007-07-25 00:24:17 +0000487
Chris Lattner4b009652007-07-25 00:24:17 +0000488 if (OpKind == tok::arrow) {
Chris Lattner7931f4a2007-07-31 16:53:04 +0000489 if (const PointerType *PT = BaseType->getAsPointerType())
Steve Naroff2cb66382007-07-26 03:11:44 +0000490 BaseType = PT->getPointeeType();
491 else
492 return Diag(OpLoc, diag::err_typecheck_member_reference_arrow,
493 SourceRange(MemberLoc));
Chris Lattner4b009652007-07-25 00:24:17 +0000494 }
Steve Naroff1b8a46c2007-07-27 22:15:19 +0000495 // The base type is either a record or an OCUVectorType.
Chris Lattnere35a1042007-07-31 19:29:30 +0000496 if (const RecordType *RTy = BaseType->getAsRecordType()) {
Steve Naroff2cb66382007-07-26 03:11:44 +0000497 RecordDecl *RDecl = RTy->getDecl();
498 if (RTy->isIncompleteType())
499 return Diag(OpLoc, diag::err_typecheck_incomplete_tag, RDecl->getName(),
500 BaseExpr->getSourceRange());
501 // The record definition is complete, now make sure the member is valid.
Steve Naroff1b8a46c2007-07-27 22:15:19 +0000502 FieldDecl *MemberDecl = RDecl->getMember(&Member);
503 if (!MemberDecl)
Steve Naroff2cb66382007-07-26 03:11:44 +0000504 return Diag(OpLoc, diag::err_typecheck_no_member, Member.getName(),
505 SourceRange(MemberLoc));
Steve Naroff1b8a46c2007-07-27 22:15:19 +0000506 return new MemberExpr(BaseExpr, OpKind==tok::arrow, MemberDecl, MemberLoc);
507 } else if (BaseType->isOCUVectorType() && OpKind == tok::period) {
Steve Naroff89345522007-08-03 22:40:33 +0000508 // Component access limited to variables (reject vec4.rg.g).
509 if (!isa<DeclRefExpr>(BaseExpr))
510 return Diag(OpLoc, diag::err_ocuvector_component_access,
511 SourceRange(MemberLoc));
Steve Naroff1b8a46c2007-07-27 22:15:19 +0000512 QualType ret = CheckOCUVectorComponent(BaseType, OpLoc, Member, MemberLoc);
513 if (ret.isNull())
514 return true;
Chris Lattnera0d03a72007-08-03 17:31:20 +0000515 return new OCUVectorElementExpr(ret, BaseExpr, Member, MemberLoc);
Steve Naroff2cb66382007-07-26 03:11:44 +0000516 } else
517 return Diag(OpLoc, diag::err_typecheck_member_reference_structUnion,
518 SourceRange(MemberLoc));
Chris Lattner4b009652007-07-25 00:24:17 +0000519}
520
Steve Naroff87d58b42007-09-16 03:34:24 +0000521/// ActOnCallExpr - Handle a call to Fn with the specified array of arguments.
Chris Lattner4b009652007-07-25 00:24:17 +0000522/// This provides the location of the left/right parens and a list of comma
523/// locations.
524Action::ExprResult Sema::
Steve Naroff87d58b42007-09-16 03:34:24 +0000525ActOnCallExpr(ExprTy *fn, SourceLocation LParenLoc,
Chris Lattner4b009652007-07-25 00:24:17 +0000526 ExprTy **args, unsigned NumArgsInCall,
527 SourceLocation *CommaLocs, SourceLocation RParenLoc) {
528 Expr *Fn = static_cast<Expr *>(fn);
529 Expr **Args = reinterpret_cast<Expr**>(args);
530 assert(Fn && "no function call expression");
531
532 UsualUnaryConversions(Fn);
533 QualType funcType = Fn->getType();
534
535 // C99 6.5.2.2p1 - "The expression that denotes the called function shall have
536 // type pointer to function".
Chris Lattner71225142007-07-31 21:27:01 +0000537 const PointerType *PT = funcType->getAsPointerType();
Chris Lattner4b009652007-07-25 00:24:17 +0000538 if (PT == 0)
539 return Diag(Fn->getLocStart(), diag::err_typecheck_call_not_function,
540 SourceRange(Fn->getLocStart(), RParenLoc));
541
Chris Lattner71225142007-07-31 21:27:01 +0000542 const FunctionType *funcT = PT->getPointeeType()->getAsFunctionType();
Chris Lattner4b009652007-07-25 00:24:17 +0000543 if (funcT == 0)
544 return Diag(Fn->getLocStart(), diag::err_typecheck_call_not_function,
545 SourceRange(Fn->getLocStart(), RParenLoc));
546
547 // If a prototype isn't declared, the parser implicitly defines a func decl
548 QualType resultType = funcT->getResultType();
549
550 if (const FunctionTypeProto *proto = dyn_cast<FunctionTypeProto>(funcT)) {
551 // C99 6.5.2.2p7 - the arguments are implicitly converted, as if by
552 // assignment, to the types of the corresponding parameter, ...
553
554 unsigned NumArgsInProto = proto->getNumArgs();
555 unsigned NumArgsToCheck = NumArgsInCall;
556
557 if (NumArgsInCall < NumArgsInProto)
558 Diag(RParenLoc, diag::err_typecheck_call_too_few_args,
559 Fn->getSourceRange());
560 else if (NumArgsInCall > NumArgsInProto) {
561 if (!proto->isVariadic()) {
562 Diag(Args[NumArgsInProto]->getLocStart(),
563 diag::err_typecheck_call_too_many_args, Fn->getSourceRange(),
564 SourceRange(Args[NumArgsInProto]->getLocStart(),
565 Args[NumArgsInCall-1]->getLocEnd()));
566 }
567 NumArgsToCheck = NumArgsInProto;
568 }
569 // Continue to check argument types (even if we have too few/many args).
570 for (unsigned i = 0; i < NumArgsToCheck; i++) {
571 Expr *argExpr = Args[i];
Steve Naroff87d58b42007-09-16 03:34:24 +0000572 assert(argExpr && "ActOnCallExpr(): missing argument expression");
Chris Lattner4b009652007-07-25 00:24:17 +0000573
574 QualType lhsType = proto->getArgType(i);
575 QualType rhsType = argExpr->getType();
576
Steve Naroff75644062007-07-25 20:45:33 +0000577 // If necessary, apply function/array conversion. C99 6.7.5.3p[7,8].
Chris Lattnere35a1042007-07-31 19:29:30 +0000578 if (const ArrayType *ary = lhsType->getAsArrayType())
Chris Lattner4b009652007-07-25 00:24:17 +0000579 lhsType = Context.getPointerType(ary->getElementType());
Steve Naroff75644062007-07-25 20:45:33 +0000580 else if (lhsType->isFunctionType())
Chris Lattner4b009652007-07-25 00:24:17 +0000581 lhsType = Context.getPointerType(lhsType);
582
583 AssignmentCheckResult result = CheckSingleAssignmentConstraints(lhsType,
584 argExpr);
Steve Naroff0f32f432007-08-24 22:33:52 +0000585 if (Args[i] != argExpr) // The expression was converted.
586 Args[i] = argExpr; // Make sure we store the converted expression.
Chris Lattner4b009652007-07-25 00:24:17 +0000587 SourceLocation l = argExpr->getLocStart();
588
589 // decode the result (notice that AST's are still created for extensions).
590 switch (result) {
591 case Compatible:
592 break;
593 case PointerFromInt:
594 // check for null pointer constant (C99 6.3.2.3p3)
595 if (!argExpr->isNullPointerConstant(Context)) {
596 Diag(l, diag::ext_typecheck_passing_pointer_int,
597 lhsType.getAsString(), rhsType.getAsString(),
598 Fn->getSourceRange(), argExpr->getSourceRange());
599 }
600 break;
601 case IntFromPointer:
602 Diag(l, diag::ext_typecheck_passing_pointer_int,
603 lhsType.getAsString(), rhsType.getAsString(),
604 Fn->getSourceRange(), argExpr->getSourceRange());
605 break;
606 case IncompatiblePointer:
607 Diag(l, diag::ext_typecheck_passing_incompatible_pointer,
608 rhsType.getAsString(), lhsType.getAsString(),
609 Fn->getSourceRange(), argExpr->getSourceRange());
610 break;
611 case CompatiblePointerDiscardsQualifiers:
612 Diag(l, diag::ext_typecheck_passing_discards_qualifiers,
613 rhsType.getAsString(), lhsType.getAsString(),
614 Fn->getSourceRange(), argExpr->getSourceRange());
615 break;
616 case Incompatible:
617 return Diag(l, diag::err_typecheck_passing_incompatible,
618 rhsType.getAsString(), lhsType.getAsString(),
619 Fn->getSourceRange(), argExpr->getSourceRange());
620 }
621 }
Steve Naroffdb65e052007-08-28 23:30:39 +0000622 if (NumArgsInCall > NumArgsInProto && proto->isVariadic()) {
623 // Promote the arguments (C99 6.5.2.2p7).
624 for (unsigned i = NumArgsInProto; i < NumArgsInCall; i++) {
625 Expr *argExpr = Args[i];
Steve Naroff87d58b42007-09-16 03:34:24 +0000626 assert(argExpr && "ActOnCallExpr(): missing argument expression");
Steve Naroffdb65e052007-08-28 23:30:39 +0000627
628 DefaultArgumentPromotion(argExpr);
629 if (Args[i] != argExpr) // The expression was converted.
630 Args[i] = argExpr; // Make sure we store the converted expression.
631 }
632 } else if (NumArgsInCall != NumArgsInProto && !proto->isVariadic()) {
633 // Even if the types checked, bail if the number of arguments don't match.
Chris Lattner4b009652007-07-25 00:24:17 +0000634 return true;
Steve Naroffdb65e052007-08-28 23:30:39 +0000635 }
636 } else if (isa<FunctionTypeNoProto>(funcT)) {
637 // Promote the arguments (C99 6.5.2.2p6).
638 for (unsigned i = 0; i < NumArgsInCall; i++) {
639 Expr *argExpr = Args[i];
Steve Naroff87d58b42007-09-16 03:34:24 +0000640 assert(argExpr && "ActOnCallExpr(): missing argument expression");
Steve Naroffdb65e052007-08-28 23:30:39 +0000641
642 DefaultArgumentPromotion(argExpr);
643 if (Args[i] != argExpr) // The expression was converted.
644 Args[i] = argExpr; // Make sure we store the converted expression.
645 }
Chris Lattner4b009652007-07-25 00:24:17 +0000646 }
Chris Lattner2e64c072007-08-10 20:18:51 +0000647 // Do special checking on direct calls to functions.
648 if (ImplicitCastExpr *IcExpr = dyn_cast<ImplicitCastExpr>(Fn))
649 if (DeclRefExpr *DRExpr = dyn_cast<DeclRefExpr>(IcExpr->getSubExpr()))
650 if (FunctionDecl *FDecl = dyn_cast<FunctionDecl>(DRExpr->getDecl()))
Chris Lattner0d9bcea2007-08-30 17:45:32 +0000651 if (CheckFunctionCall(Fn, LParenLoc, RParenLoc, FDecl, Args,
652 NumArgsInCall))
Anders Carlssone7e7aa22007-08-17 05:31:46 +0000653 return true;
Chris Lattner2e64c072007-08-10 20:18:51 +0000654
Chris Lattner4b009652007-07-25 00:24:17 +0000655 return new CallExpr(Fn, Args, NumArgsInCall, resultType, RParenLoc);
656}
657
658Action::ExprResult Sema::
Steve Naroff87d58b42007-09-16 03:34:24 +0000659ActOnCompoundLiteral(SourceLocation LParenLoc, TypeTy *Ty,
Chris Lattner4b009652007-07-25 00:24:17 +0000660 SourceLocation RParenLoc, ExprTy *InitExpr) {
Steve Naroff87d58b42007-09-16 03:34:24 +0000661 assert((Ty != 0) && "ActOnCompoundLiteral(): missing type");
Chris Lattner4b009652007-07-25 00:24:17 +0000662 QualType literalType = QualType::getFromOpaquePtr(Ty);
663 // FIXME: put back this assert when initializers are worked out.
Steve Naroff87d58b42007-09-16 03:34:24 +0000664 //assert((InitExpr != 0) && "ActOnCompoundLiteral(): missing expression");
Chris Lattner4b009652007-07-25 00:24:17 +0000665 Expr *literalExpr = static_cast<Expr*>(InitExpr);
666
667 // FIXME: add semantic analysis (C99 6.5.2.5).
668 return new CompoundLiteralExpr(literalType, literalExpr);
669}
670
671Action::ExprResult Sema::
Steve Naroff87d58b42007-09-16 03:34:24 +0000672ActOnInitList(SourceLocation LBraceLoc, ExprTy **initlist, unsigned NumInit,
Anders Carlsson762b7c72007-08-31 04:56:16 +0000673 SourceLocation RBraceLoc) {
Steve Naroffe14e5542007-09-02 02:04:30 +0000674 Expr **InitList = reinterpret_cast<Expr**>(initlist);
Anders Carlsson762b7c72007-08-31 04:56:16 +0000675
Steve Naroff0acc9c92007-09-15 18:49:24 +0000676 // Semantic analysis for initializers is done by ActOnDeclarator() and
Steve Naroff1c9de712007-09-03 01:24:23 +0000677 // CheckInitializer() - it requires knowledge of the object being intialized.
Anders Carlsson762b7c72007-08-31 04:56:16 +0000678
Steve Naroff7c9d72d2007-09-02 20:30:18 +0000679 InitListExpr *e = new InitListExpr(LBraceLoc, InitList, NumInit, RBraceLoc);
680 e->setType(Context.VoidTy); // FIXME: just a place holder for now.
681 return e;
Chris Lattner4b009652007-07-25 00:24:17 +0000682}
683
684Action::ExprResult Sema::
Steve Naroff87d58b42007-09-16 03:34:24 +0000685ActOnCastExpr(SourceLocation LParenLoc, TypeTy *Ty,
Chris Lattner4b009652007-07-25 00:24:17 +0000686 SourceLocation RParenLoc, ExprTy *Op) {
Steve Naroff87d58b42007-09-16 03:34:24 +0000687 assert((Ty != 0) && (Op != 0) && "ActOnCastExpr(): missing type or expr");
Chris Lattner4b009652007-07-25 00:24:17 +0000688
689 Expr *castExpr = static_cast<Expr*>(Op);
690 QualType castType = QualType::getFromOpaquePtr(Ty);
691
Steve Naroff68adb482007-08-31 00:32:44 +0000692 UsualUnaryConversions(castExpr);
693
Chris Lattner4b009652007-07-25 00:24:17 +0000694 // C99 6.5.4p2: the cast type needs to be void or scalar and the expression
695 // type needs to be scalar.
696 if (!castType->isScalarType() && !castType->isVoidType()) {
697 return Diag(LParenLoc, diag::err_typecheck_cond_expect_scalar,
698 castType.getAsString(), SourceRange(LParenLoc, RParenLoc));
699 }
700 if (!castExpr->getType()->isScalarType()) {
701 return Diag(castExpr->getLocStart(),
702 diag::err_typecheck_expect_scalar_operand,
703 castExpr->getType().getAsString(), castExpr->getSourceRange());
704 }
705 return new CastExpr(castType, castExpr, LParenLoc);
706}
707
Steve Naroff144667e2007-10-18 05:13:08 +0000708// promoteExprToType - a helper function to ensure we create exactly one
709// ImplicitCastExpr.
710static void promoteExprToType(Expr *&expr, QualType type) {
711 if (ImplicitCastExpr *impCast = dyn_cast<ImplicitCastExpr>(expr))
712 impCast->setType(type);
713 else
714 expr = new ImplicitCastExpr(type, expr);
715 return;
716}
717
Chris Lattner4b009652007-07-25 00:24:17 +0000718inline QualType Sema::CheckConditionalOperands( // C99 6.5.15
719 Expr *&cond, Expr *&lex, Expr *&rex, SourceLocation questionLoc) {
720 UsualUnaryConversions(cond);
721 UsualUnaryConversions(lex);
722 UsualUnaryConversions(rex);
723 QualType condT = cond->getType();
724 QualType lexT = lex->getType();
725 QualType rexT = rex->getType();
726
727 // first, check the condition.
728 if (!condT->isScalarType()) { // C99 6.5.15p2
729 Diag(cond->getLocStart(), diag::err_typecheck_cond_expect_scalar,
730 condT.getAsString());
731 return QualType();
732 }
733 // now check the two expressions.
734 if (lexT->isArithmeticType() && rexT->isArithmeticType()) { // C99 6.5.15p3,5
735 UsualArithmeticConversions(lex, rex);
736 return lex->getType();
737 }
Chris Lattner71225142007-07-31 21:27:01 +0000738 if (const RecordType *LHSRT = lexT->getAsRecordType()) { // C99 6.5.15p3
739 if (const RecordType *RHSRT = rexT->getAsRecordType()) {
740
Chris Lattner0d9bcea2007-08-30 17:45:32 +0000741 if (LHSRT->getDecl()->getIdentifier() ==RHSRT->getDecl()->getIdentifier())
Chris Lattner71225142007-07-31 21:27:01 +0000742 return lexT;
743
Chris Lattner4b009652007-07-25 00:24:17 +0000744 Diag(questionLoc, diag::err_typecheck_cond_incompatible_operands,
745 lexT.getAsString(), rexT.getAsString(),
746 lex->getSourceRange(), rex->getSourceRange());
747 return QualType();
748 }
749 }
750 // C99 6.5.15p3
Steve Naroff144667e2007-10-18 05:13:08 +0000751 if (lexT->isPointerType() && rex->isNullPointerConstant(Context)) {
752 promoteExprToType(rex, lexT); // promote the null to a pointer.
Chris Lattner4b009652007-07-25 00:24:17 +0000753 return lexT;
Steve Naroff144667e2007-10-18 05:13:08 +0000754 }
755 if (rexT->isPointerType() && lex->isNullPointerConstant(Context)) {
756 promoteExprToType(lex, rexT); // promote the null to a pointer.
Chris Lattner4b009652007-07-25 00:24:17 +0000757 return rexT;
Steve Naroff144667e2007-10-18 05:13:08 +0000758 }
Chris Lattner71225142007-07-31 21:27:01 +0000759 if (const PointerType *LHSPT = lexT->getAsPointerType()) { // C99 6.5.15p3,6
760 if (const PointerType *RHSPT = rexT->getAsPointerType()) {
761 // get the "pointed to" types
762 QualType lhptee = LHSPT->getPointeeType();
763 QualType rhptee = RHSPT->getPointeeType();
Chris Lattner4b009652007-07-25 00:24:17 +0000764
Chris Lattner71225142007-07-31 21:27:01 +0000765 // ignore qualifiers on void (C99 6.5.15p3, clause 6)
766 if (lhptee->isVoidType() &&
767 (rhptee->isObjectType() || rhptee->isIncompleteType()))
768 return lexT;
769 if (rhptee->isVoidType() &&
770 (lhptee->isObjectType() || lhptee->isIncompleteType()))
771 return rexT;
Chris Lattner4b009652007-07-25 00:24:17 +0000772
Steve Naroff85f0dc52007-10-15 20:41:53 +0000773 if (!Context.typesAreCompatible(lhptee.getUnqualifiedType(),
774 rhptee.getUnqualifiedType())) {
Chris Lattner71225142007-07-31 21:27:01 +0000775 Diag(questionLoc, diag::ext_typecheck_cond_incompatible_pointers,
776 lexT.getAsString(), rexT.getAsString(),
777 lex->getSourceRange(), rex->getSourceRange());
778 return lexT; // FIXME: this is an _ext - is this return o.k?
779 }
780 // The pointer types are compatible.
Chris Lattner0d9bcea2007-08-30 17:45:32 +0000781 // C99 6.5.15p6: If both operands are pointers to compatible types *or* to
782 // differently qualified versions of compatible types, the result type is
783 // a pointer to an appropriately qualified version of the *composite*
784 // type.
Chris Lattner71225142007-07-31 21:27:01 +0000785 return lexT; // FIXME: Need to return the composite type.
Chris Lattner4b009652007-07-25 00:24:17 +0000786 }
Chris Lattner4b009652007-07-25 00:24:17 +0000787 }
Chris Lattner71225142007-07-31 21:27:01 +0000788
Chris Lattner4b009652007-07-25 00:24:17 +0000789 if (lexT->isVoidType() && rexT->isVoidType()) // C99 6.5.15p3
790 return lexT;
791
792 Diag(questionLoc, diag::err_typecheck_cond_incompatible_operands,
793 lexT.getAsString(), rexT.getAsString(),
794 lex->getSourceRange(), rex->getSourceRange());
795 return QualType();
796}
797
Steve Naroff87d58b42007-09-16 03:34:24 +0000798/// ActOnConditionalOp - Parse a ?: operation. Note that 'LHS' may be null
Chris Lattner4b009652007-07-25 00:24:17 +0000799/// in the case of a the GNU conditional expr extension.
Steve Naroff87d58b42007-09-16 03:34:24 +0000800Action::ExprResult Sema::ActOnConditionalOp(SourceLocation QuestionLoc,
Chris Lattner4b009652007-07-25 00:24:17 +0000801 SourceLocation ColonLoc,
802 ExprTy *Cond, ExprTy *LHS,
803 ExprTy *RHS) {
804 Expr *CondExpr = (Expr *) Cond;
805 Expr *LHSExpr = (Expr *) LHS, *RHSExpr = (Expr *) RHS;
806 QualType result = CheckConditionalOperands(CondExpr, LHSExpr,
807 RHSExpr, QuestionLoc);
808 if (result.isNull())
809 return true;
810 return new ConditionalOperator(CondExpr, LHSExpr, RHSExpr, result);
811}
812
Steve Naroffdb65e052007-08-28 23:30:39 +0000813/// DefaultArgumentPromotion (C99 6.5.2.2p6). Used for function calls that
814/// do not have a prototype. Integer promotions are performed on each
815/// argument, and arguments that have type float are promoted to double.
816void Sema::DefaultArgumentPromotion(Expr *&expr) {
817 QualType t = expr->getType();
818 assert(!t.isNull() && "DefaultArgumentPromotion - missing type");
819
820 if (t->isPromotableIntegerType()) // C99 6.3.1.1p2
821 promoteExprToType(expr, Context.IntTy);
822 if (t == Context.FloatTy)
823 promoteExprToType(expr, Context.DoubleTy);
824}
825
Chris Lattner4b009652007-07-25 00:24:17 +0000826/// DefaultFunctionArrayConversion (C99 6.3.2.1p3, C99 6.3.2.1p4).
827void Sema::DefaultFunctionArrayConversion(Expr *&e) {
828 QualType t = e->getType();
829 assert(!t.isNull() && "DefaultFunctionArrayConversion - missing type");
830
Chris Lattnerf0c4a0a2007-07-31 16:56:34 +0000831 if (const ReferenceType *ref = t->getAsReferenceType()) {
Chris Lattner4b009652007-07-25 00:24:17 +0000832 promoteExprToType(e, ref->getReferenceeType()); // C++ [expr]
833 t = e->getType();
834 }
835 if (t->isFunctionType())
836 promoteExprToType(e, Context.getPointerType(t));
Chris Lattnere35a1042007-07-31 19:29:30 +0000837 else if (const ArrayType *ary = t->getAsArrayType())
Chris Lattner4b009652007-07-25 00:24:17 +0000838 promoteExprToType(e, Context.getPointerType(ary->getElementType()));
839}
840
841/// UsualUnaryConversion - Performs various conversions that are common to most
842/// operators (C99 6.3). The conversions of array and function types are
843/// sometimes surpressed. For example, the array->pointer conversion doesn't
844/// apply if the array is an argument to the sizeof or address (&) operators.
845/// In these instances, this routine should *not* be called.
846void Sema::UsualUnaryConversions(Expr *&expr) {
847 QualType t = expr->getType();
848 assert(!t.isNull() && "UsualUnaryConversions - missing type");
849
Chris Lattnerf0c4a0a2007-07-31 16:56:34 +0000850 if (const ReferenceType *ref = t->getAsReferenceType()) {
Chris Lattner4b009652007-07-25 00:24:17 +0000851 promoteExprToType(expr, ref->getReferenceeType()); // C++ [expr]
852 t = expr->getType();
853 }
854 if (t->isPromotableIntegerType()) // C99 6.3.1.1p2
855 promoteExprToType(expr, Context.IntTy);
856 else
857 DefaultFunctionArrayConversion(expr);
858}
859
Chris Lattner0d9bcea2007-08-30 17:45:32 +0000860/// UsualArithmeticConversions - Performs various conversions that are common to
Chris Lattner4b009652007-07-25 00:24:17 +0000861/// binary operators (C99 6.3.1.8). If both operands aren't arithmetic, this
862/// routine returns the first non-arithmetic type found. The client is
863/// responsible for emitting appropriate error diagnostics.
Steve Naroff8f708362007-08-24 19:07:16 +0000864QualType Sema::UsualArithmeticConversions(Expr *&lhsExpr, Expr *&rhsExpr,
865 bool isCompAssign) {
Steve Naroffb2f9f552007-08-25 19:54:59 +0000866 if (!isCompAssign) {
867 UsualUnaryConversions(lhsExpr);
868 UsualUnaryConversions(rhsExpr);
869 }
Chris Lattner4b009652007-07-25 00:24:17 +0000870 QualType lhs = lhsExpr->getType();
871 QualType rhs = rhsExpr->getType();
872
873 // If both types are identical, no conversion is needed.
Chris Lattner69318302007-10-18 03:50:33 +0000874 if (lhs.getTypePtr() == rhs.getTypePtr())
875 return lhs.getQualifiedType(0);
Chris Lattner4b009652007-07-25 00:24:17 +0000876
877 // If either side is a non-arithmetic type (e.g. a pointer), we are done.
878 // The caller can deal with this (e.g. pointer + int).
879 if (!lhs->isArithmeticType() || !rhs->isArithmeticType())
Steve Naroff8f708362007-08-24 19:07:16 +0000880 return lhs;
Chris Lattner4b009652007-07-25 00:24:17 +0000881
882 // At this point, we have two different arithmetic types.
883
884 // Handle complex types first (C99 6.3.1.8p1).
885 if (lhs->isComplexType() || rhs->isComplexType()) {
886 // if we have an integer operand, the result is the complex type.
887 if (rhs->isIntegerType()) { // convert the rhs to the lhs complex type.
Steve Naroff8f708362007-08-24 19:07:16 +0000888 if (!isCompAssign) promoteExprToType(rhsExpr, lhs);
889 return lhs;
Chris Lattner4b009652007-07-25 00:24:17 +0000890 }
891 if (lhs->isIntegerType()) { // convert the lhs to the rhs complex type.
Steve Naroff8f708362007-08-24 19:07:16 +0000892 if (!isCompAssign) promoteExprToType(lhsExpr, rhs);
893 return rhs;
Chris Lattner4b009652007-07-25 00:24:17 +0000894 }
Steve Naroff3cf497f2007-08-27 01:27:54 +0000895 // This handles complex/complex, complex/float, or float/complex.
896 // When both operands are complex, the shorter operand is converted to the
897 // type of the longer, and that is the type of the result. This corresponds
898 // to what is done when combining two real floating-point operands.
899 // The fun begins when size promotion occur across type domains.
900 // From H&S 6.3.4: When one operand is complex and the other is a real
901 // floating-point type, the less precise type is converted, within it's
902 // real or complex domain, to the precision of the other type. For example,
903 // when combining a "long double" with a "double _Complex", the
904 // "double _Complex" is promoted to "long double _Complex".
Steve Naroff45fc9822007-08-27 15:30:22 +0000905 int result = Context.compareFloatingType(lhs, rhs);
906
907 if (result > 0) { // The left side is bigger, convert rhs.
Steve Naroff3b565d62007-08-27 21:32:55 +0000908 rhs = Context.getFloatingTypeOfSizeWithinDomain(lhs, rhs);
909 if (!isCompAssign)
910 promoteExprToType(rhsExpr, rhs);
911 } else if (result < 0) { // The right side is bigger, convert lhs.
912 lhs = Context.getFloatingTypeOfSizeWithinDomain(rhs, lhs);
913 if (!isCompAssign)
914 promoteExprToType(lhsExpr, lhs);
915 }
916 // At this point, lhs and rhs have the same rank/size. Now, make sure the
917 // domains match. This is a requirement for our implementation, C99
918 // does not require this promotion.
919 if (lhs != rhs) { // Domains don't match, we have complex/float mix.
920 if (lhs->isRealFloatingType()) { // handle "double, _Complex double".
Steve Naroff3b6157f2007-08-27 21:43:43 +0000921 if (!isCompAssign)
922 promoteExprToType(lhsExpr, rhs);
923 return rhs;
Steve Naroff3b565d62007-08-27 21:32:55 +0000924 } else { // handle "_Complex double, double".
Steve Naroff3b6157f2007-08-27 21:43:43 +0000925 if (!isCompAssign)
926 promoteExprToType(rhsExpr, lhs);
927 return lhs;
Steve Naroff3b565d62007-08-27 21:32:55 +0000928 }
Chris Lattner4b009652007-07-25 00:24:17 +0000929 }
Steve Naroff3b6157f2007-08-27 21:43:43 +0000930 return lhs; // The domain/size match exactly.
Chris Lattner4b009652007-07-25 00:24:17 +0000931 }
932 // Now handle "real" floating types (i.e. float, double, long double).
933 if (lhs->isRealFloatingType() || rhs->isRealFloatingType()) {
934 // if we have an integer operand, the result is the real floating type.
935 if (rhs->isIntegerType()) { // convert rhs to the lhs floating point type.
Steve Naroff8f708362007-08-24 19:07:16 +0000936 if (!isCompAssign) promoteExprToType(rhsExpr, lhs);
937 return lhs;
Chris Lattner4b009652007-07-25 00:24:17 +0000938 }
939 if (lhs->isIntegerType()) { // convert lhs to the rhs floating point type.
Steve Naroff8f708362007-08-24 19:07:16 +0000940 if (!isCompAssign) promoteExprToType(lhsExpr, rhs);
941 return rhs;
Chris Lattner4b009652007-07-25 00:24:17 +0000942 }
943 // We have two real floating types, float/complex combos were handled above.
944 // Convert the smaller operand to the bigger result.
Steve Naroff45fc9822007-08-27 15:30:22 +0000945 int result = Context.compareFloatingType(lhs, rhs);
946
947 if (result > 0) { // convert the rhs
Steve Naroff8f708362007-08-24 19:07:16 +0000948 if (!isCompAssign) promoteExprToType(rhsExpr, lhs);
949 return lhs;
Chris Lattner4b009652007-07-25 00:24:17 +0000950 }
Steve Naroff45fc9822007-08-27 15:30:22 +0000951 if (result < 0) { // convert the lhs
952 if (!isCompAssign) promoteExprToType(lhsExpr, rhs); // convert the lhs
953 return rhs;
954 }
955 assert(0 && "Sema::UsualArithmeticConversions(): illegal float comparison");
Chris Lattner4b009652007-07-25 00:24:17 +0000956 }
957 // Finally, we have two differing integer types.
958 if (Context.maxIntegerType(lhs, rhs) == lhs) { // convert the rhs
Steve Naroff8f708362007-08-24 19:07:16 +0000959 if (!isCompAssign) promoteExprToType(rhsExpr, lhs);
960 return lhs;
Chris Lattner4b009652007-07-25 00:24:17 +0000961 }
Steve Naroff8f708362007-08-24 19:07:16 +0000962 if (!isCompAssign) promoteExprToType(lhsExpr, rhs); // convert the lhs
963 return rhs;
Chris Lattner4b009652007-07-25 00:24:17 +0000964}
965
966// CheckPointerTypesForAssignment - This is a very tricky routine (despite
967// being closely modeled after the C99 spec:-). The odd characteristic of this
968// routine is it effectively iqnores the qualifiers on the top level pointee.
969// This circumvents the usual type rules specified in 6.2.7p1 & 6.7.5.[1-3].
970// FIXME: add a couple examples in this comment.
971Sema::AssignmentCheckResult
972Sema::CheckPointerTypesForAssignment(QualType lhsType, QualType rhsType) {
973 QualType lhptee, rhptee;
974
975 // get the "pointed to" type (ignoring qualifiers at the top level)
Chris Lattner71225142007-07-31 21:27:01 +0000976 lhptee = lhsType->getAsPointerType()->getPointeeType();
977 rhptee = rhsType->getAsPointerType()->getPointeeType();
Chris Lattner4b009652007-07-25 00:24:17 +0000978
979 // make sure we operate on the canonical type
980 lhptee = lhptee.getCanonicalType();
981 rhptee = rhptee.getCanonicalType();
982
983 AssignmentCheckResult r = Compatible;
984
985 // C99 6.5.16.1p1: This following citation is common to constraints
986 // 3 & 4 (below). ...and the type *pointed to* by the left has all the
987 // qualifiers of the type *pointed to* by the right;
988 if ((lhptee.getQualifiers() & rhptee.getQualifiers()) !=
989 rhptee.getQualifiers())
990 r = CompatiblePointerDiscardsQualifiers;
991
992 // C99 6.5.16.1p1 (constraint 4): If one operand is a pointer to an object or
993 // incomplete type and the other is a pointer to a qualified or unqualified
994 // version of void...
995 if (lhptee.getUnqualifiedType()->isVoidType() &&
996 (rhptee->isObjectType() || rhptee->isIncompleteType()))
997 ;
998 else if (rhptee.getUnqualifiedType()->isVoidType() &&
999 (lhptee->isObjectType() || lhptee->isIncompleteType()))
1000 ;
1001 // C99 6.5.16.1p1 (constraint 3): both operands are pointers to qualified or
1002 // unqualified versions of compatible types, ...
Steve Naroff85f0dc52007-10-15 20:41:53 +00001003 else if (!Context.typesAreCompatible(lhptee.getUnqualifiedType(),
1004 rhptee.getUnqualifiedType()))
Chris Lattner4b009652007-07-25 00:24:17 +00001005 r = IncompatiblePointer; // this "trumps" PointerAssignDiscardsQualifiers
1006 return r;
1007}
1008
1009/// CheckAssignmentConstraints (C99 6.5.16) - This routine currently
1010/// has code to accommodate several GCC extensions when type checking
1011/// pointers. Here are some objectionable examples that GCC considers warnings:
1012///
1013/// int a, *pint;
1014/// short *pshort;
1015/// struct foo *pfoo;
1016///
1017/// pint = pshort; // warning: assignment from incompatible pointer type
1018/// a = pint; // warning: assignment makes integer from pointer without a cast
1019/// pint = a; // warning: assignment makes pointer from integer without a cast
1020/// pint = pfoo; // warning: assignment from incompatible pointer type
1021///
1022/// As a result, the code for dealing with pointers is more complex than the
1023/// C99 spec dictates.
1024/// Note: the warning above turn into errors when -pedantic-errors is enabled.
1025///
1026Sema::AssignmentCheckResult
1027Sema::CheckAssignmentConstraints(QualType lhsType, QualType rhsType) {
1028 if (lhsType == rhsType) // common case, fast path...
1029 return Compatible;
1030
Anders Carlssoncebb8d62007-10-12 23:56:29 +00001031 if (lhsType->isReferenceType() || rhsType->isReferenceType()) {
Steve Naroff85f0dc52007-10-15 20:41:53 +00001032 if (Context.referenceTypesAreCompatible(lhsType, rhsType))
Anders Carlssoncebb8d62007-10-12 23:56:29 +00001033 return Compatible;
1034 } else if (lhsType->isArithmeticType() && rhsType->isArithmeticType()) {
Chris Lattner4b009652007-07-25 00:24:17 +00001035 if (lhsType->isVectorType() || rhsType->isVectorType()) {
1036 if (lhsType.getCanonicalType() != rhsType.getCanonicalType())
1037 return Incompatible;
1038 }
1039 return Compatible;
1040 } else if (lhsType->isPointerType()) {
1041 if (rhsType->isIntegerType())
1042 return PointerFromInt;
1043
1044 if (rhsType->isPointerType())
1045 return CheckPointerTypesForAssignment(lhsType, rhsType);
1046 } else if (rhsType->isPointerType()) {
1047 // C99 6.5.16.1p1: the left operand is _Bool and the right is a pointer.
1048 if ((lhsType->isIntegerType()) && (lhsType != Context.BoolTy))
1049 return IntFromPointer;
1050
1051 if (lhsType->isPointerType())
1052 return CheckPointerTypesForAssignment(lhsType, rhsType);
1053 } else if (isa<TagType>(lhsType) && isa<TagType>(rhsType)) {
Steve Naroff85f0dc52007-10-15 20:41:53 +00001054 if (Context.tagTypesAreCompatible(lhsType, rhsType))
Chris Lattner4b009652007-07-25 00:24:17 +00001055 return Compatible;
Chris Lattner4b009652007-07-25 00:24:17 +00001056 }
1057 return Incompatible;
1058}
1059
1060Sema::AssignmentCheckResult
1061Sema::CheckSingleAssignmentConstraints(QualType lhsType, Expr *&rExpr) {
Chris Lattner5f505bf2007-10-16 02:55:40 +00001062 // This check seems unnatural, however it is necessary to ensure the proper
Chris Lattner4b009652007-07-25 00:24:17 +00001063 // conversion of functions/arrays. If the conversion were done for all
Steve Naroff0acc9c92007-09-15 18:49:24 +00001064 // DeclExpr's (created by ActOnIdentifierExpr), it would mess up the unary
Chris Lattner4b009652007-07-25 00:24:17 +00001065 // expressions that surpress this implicit conversion (&, sizeof).
Chris Lattner5f505bf2007-10-16 02:55:40 +00001066 //
1067 // Suppress this for references: C99 8.5.3p5. FIXME: revisit when references
1068 // are better understood.
1069 if (!lhsType->isReferenceType())
1070 DefaultFunctionArrayConversion(rExpr);
Steve Naroff0f32f432007-08-24 22:33:52 +00001071
1072 Sema::AssignmentCheckResult result;
Chris Lattner4b009652007-07-25 00:24:17 +00001073
Steve Naroff0f32f432007-08-24 22:33:52 +00001074 result = CheckAssignmentConstraints(lhsType, rExpr->getType());
1075
1076 // C99 6.5.16.1p2: The value of the right operand is converted to the
1077 // type of the assignment expression.
1078 if (rExpr->getType() != lhsType)
1079 promoteExprToType(rExpr, lhsType);
1080 return result;
Chris Lattner4b009652007-07-25 00:24:17 +00001081}
1082
1083Sema::AssignmentCheckResult
1084Sema::CheckCompoundAssignmentConstraints(QualType lhsType, QualType rhsType) {
1085 return CheckAssignmentConstraints(lhsType, rhsType);
1086}
1087
1088inline void Sema::InvalidOperands(SourceLocation loc, Expr *&lex, Expr *&rex) {
1089 Diag(loc, diag::err_typecheck_invalid_operands,
1090 lex->getType().getAsString(), rex->getType().getAsString(),
1091 lex->getSourceRange(), rex->getSourceRange());
1092}
1093
1094inline QualType Sema::CheckVectorOperands(SourceLocation loc, Expr *&lex,
1095 Expr *&rex) {
1096 QualType lhsType = lex->getType(), rhsType = rex->getType();
1097
1098 // make sure the vector types are identical.
1099 if (lhsType == rhsType)
1100 return lhsType;
1101 // You cannot convert between vector values of different size.
1102 Diag(loc, diag::err_typecheck_vector_not_convertable,
1103 lex->getType().getAsString(), rex->getType().getAsString(),
1104 lex->getSourceRange(), rex->getSourceRange());
1105 return QualType();
1106}
1107
1108inline QualType Sema::CheckMultiplyDivideOperands(
Steve Naroff8f708362007-08-24 19:07:16 +00001109 Expr *&lex, Expr *&rex, SourceLocation loc, bool isCompAssign)
Chris Lattner4b009652007-07-25 00:24:17 +00001110{
1111 QualType lhsType = lex->getType(), rhsType = rex->getType();
1112
1113 if (lhsType->isVectorType() || rhsType->isVectorType())
1114 return CheckVectorOperands(loc, lex, rex);
1115
Steve Naroff8f708362007-08-24 19:07:16 +00001116 QualType compType = UsualArithmeticConversions(lex, rex, isCompAssign);
Chris Lattner4b009652007-07-25 00:24:17 +00001117
Chris Lattner4b009652007-07-25 00:24:17 +00001118 if (lex->getType()->isArithmeticType() && rex->getType()->isArithmeticType())
Steve Naroff8f708362007-08-24 19:07:16 +00001119 return compType;
Chris Lattner4b009652007-07-25 00:24:17 +00001120 InvalidOperands(loc, lex, rex);
1121 return QualType();
1122}
1123
1124inline QualType Sema::CheckRemainderOperands(
Steve Naroff8f708362007-08-24 19:07:16 +00001125 Expr *&lex, Expr *&rex, SourceLocation loc, bool isCompAssign)
Chris Lattner4b009652007-07-25 00:24:17 +00001126{
1127 QualType lhsType = lex->getType(), rhsType = rex->getType();
1128
Steve Naroff8f708362007-08-24 19:07:16 +00001129 QualType compType = UsualArithmeticConversions(lex, rex, isCompAssign);
Chris Lattner4b009652007-07-25 00:24:17 +00001130
Chris Lattner4b009652007-07-25 00:24:17 +00001131 if (lex->getType()->isIntegerType() && rex->getType()->isIntegerType())
Steve Naroff8f708362007-08-24 19:07:16 +00001132 return compType;
Chris Lattner4b009652007-07-25 00:24:17 +00001133 InvalidOperands(loc, lex, rex);
1134 return QualType();
1135}
1136
1137inline QualType Sema::CheckAdditionOperands( // C99 6.5.6
Steve Naroff8f708362007-08-24 19:07:16 +00001138 Expr *&lex, Expr *&rex, SourceLocation loc, bool isCompAssign)
Chris Lattner4b009652007-07-25 00:24:17 +00001139{
1140 if (lex->getType()->isVectorType() || rex->getType()->isVectorType())
1141 return CheckVectorOperands(loc, lex, rex);
1142
Steve Naroff8f708362007-08-24 19:07:16 +00001143 QualType compType = UsualArithmeticConversions(lex, rex, isCompAssign);
Chris Lattner4b009652007-07-25 00:24:17 +00001144
1145 // handle the common case first (both operands are arithmetic).
1146 if (lex->getType()->isArithmeticType() && rex->getType()->isArithmeticType())
Steve Naroff8f708362007-08-24 19:07:16 +00001147 return compType;
Chris Lattner4b009652007-07-25 00:24:17 +00001148
1149 if (lex->getType()->isPointerType() && rex->getType()->isIntegerType())
1150 return lex->getType();
1151 if (lex->getType()->isIntegerType() && rex->getType()->isPointerType())
1152 return rex->getType();
1153 InvalidOperands(loc, lex, rex);
1154 return QualType();
1155}
1156
1157inline QualType Sema::CheckSubtractionOperands( // C99 6.5.6
Steve Naroff8f708362007-08-24 19:07:16 +00001158 Expr *&lex, Expr *&rex, SourceLocation loc, bool isCompAssign)
Chris Lattner4b009652007-07-25 00:24:17 +00001159{
1160 if (lex->getType()->isVectorType() || rex->getType()->isVectorType())
1161 return CheckVectorOperands(loc, lex, rex);
1162
Steve Naroff8f708362007-08-24 19:07:16 +00001163 QualType compType = UsualArithmeticConversions(lex, rex, isCompAssign);
Chris Lattner4b009652007-07-25 00:24:17 +00001164
1165 // handle the common case first (both operands are arithmetic).
1166 if (lex->getType()->isArithmeticType() && rex->getType()->isArithmeticType())
Steve Naroff8f708362007-08-24 19:07:16 +00001167 return compType;
Chris Lattner4b009652007-07-25 00:24:17 +00001168
1169 if (lex->getType()->isPointerType() && rex->getType()->isIntegerType())
Steve Naroff8f708362007-08-24 19:07:16 +00001170 return compType;
Chris Lattner4b009652007-07-25 00:24:17 +00001171 if (lex->getType()->isPointerType() && rex->getType()->isPointerType())
1172 return Context.getPointerDiffType();
1173 InvalidOperands(loc, lex, rex);
1174 return QualType();
1175}
1176
1177inline QualType Sema::CheckShiftOperands( // C99 6.5.7
Steve Naroff8f708362007-08-24 19:07:16 +00001178 Expr *&lex, Expr *&rex, SourceLocation loc, bool isCompAssign)
Chris Lattner4b009652007-07-25 00:24:17 +00001179{
1180 // FIXME: Shifts don't perform usual arithmetic conversions. This is wrong
1181 // for int << longlong -> the result type should be int, not long long.
Steve Naroff8f708362007-08-24 19:07:16 +00001182 QualType compType = UsualArithmeticConversions(lex, rex, isCompAssign);
Chris Lattner4b009652007-07-25 00:24:17 +00001183
1184 // handle the common case first (both operands are arithmetic).
1185 if (lex->getType()->isIntegerType() && rex->getType()->isIntegerType())
Steve Naroff8f708362007-08-24 19:07:16 +00001186 return compType;
Chris Lattner4b009652007-07-25 00:24:17 +00001187 InvalidOperands(loc, lex, rex);
1188 return QualType();
1189}
1190
Chris Lattner254f3bc2007-08-26 01:18:55 +00001191inline QualType Sema::CheckCompareOperands( // C99 6.5.8
1192 Expr *&lex, Expr *&rex, SourceLocation loc, bool isRelational)
Chris Lattner4b009652007-07-25 00:24:17 +00001193{
Chris Lattner254f3bc2007-08-26 01:18:55 +00001194 // C99 6.5.8p3 / C99 6.5.9p4
Steve Naroffecc4fa12007-08-10 18:26:40 +00001195 if (lex->getType()->isArithmeticType() && rex->getType()->isArithmeticType())
1196 UsualArithmeticConversions(lex, rex);
1197 else {
1198 UsualUnaryConversions(lex);
1199 UsualUnaryConversions(rex);
1200 }
Chris Lattner4b009652007-07-25 00:24:17 +00001201 QualType lType = lex->getType();
1202 QualType rType = rex->getType();
1203
Chris Lattner254f3bc2007-08-26 01:18:55 +00001204 if (isRelational) {
1205 if (lType->isRealType() && rType->isRealType())
1206 return Context.IntTy;
1207 } else {
Chris Lattnerbd3cc222007-08-30 06:10:41 +00001208 if (lType->isFloatingType() && rType->isFloatingType())
Ted Kremenekec761af2007-08-29 18:06:12 +00001209 Diag(loc, diag::warn_floatingpoint_eq);
1210
Chris Lattner254f3bc2007-08-26 01:18:55 +00001211 if (lType->isArithmeticType() && rType->isArithmeticType())
1212 return Context.IntTy;
1213 }
Chris Lattner4b009652007-07-25 00:24:17 +00001214
Chris Lattner22be8422007-08-26 01:10:14 +00001215 bool LHSIsNull = lex->isNullPointerConstant(Context);
1216 bool RHSIsNull = rex->isNullPointerConstant(Context);
1217
Chris Lattner254f3bc2007-08-26 01:18:55 +00001218 // All of the following pointer related warnings are GCC extensions, except
1219 // when handling null pointer constants. One day, we can consider making them
1220 // errors (when -pedantic-errors is enabled).
Steve Naroffc33c0602007-08-27 04:08:11 +00001221 if (lType->isPointerType() && rType->isPointerType()) { // C99 6.5.8p2
Chris Lattner22be8422007-08-26 01:10:14 +00001222 if (!LHSIsNull && !RHSIsNull &&
Steve Naroff85f0dc52007-10-15 20:41:53 +00001223 !Context.pointerTypesAreCompatible(lType.getUnqualifiedType(),
1224 rType.getUnqualifiedType())) {
Steve Naroff4462cb02007-08-16 21:48:38 +00001225 Diag(loc, diag::ext_typecheck_comparison_of_distinct_pointers,
1226 lType.getAsString(), rType.getAsString(),
1227 lex->getSourceRange(), rex->getSourceRange());
Chris Lattner4b009652007-07-25 00:24:17 +00001228 }
Chris Lattner22be8422007-08-26 01:10:14 +00001229 promoteExprToType(rex, lType); // promote the pointer to pointer
Steve Naroff4462cb02007-08-16 21:48:38 +00001230 return Context.IntTy;
1231 }
1232 if (lType->isPointerType() && rType->isIntegerType()) {
Chris Lattner22be8422007-08-26 01:10:14 +00001233 if (!RHSIsNull)
Steve Naroff4462cb02007-08-16 21:48:38 +00001234 Diag(loc, diag::ext_typecheck_comparison_of_pointer_integer,
1235 lType.getAsString(), rType.getAsString(),
1236 lex->getSourceRange(), rex->getSourceRange());
Chris Lattner22be8422007-08-26 01:10:14 +00001237 promoteExprToType(rex, lType); // promote the integer to pointer
Steve Naroff4462cb02007-08-16 21:48:38 +00001238 return Context.IntTy;
1239 }
1240 if (lType->isIntegerType() && rType->isPointerType()) {
Chris Lattner22be8422007-08-26 01:10:14 +00001241 if (!LHSIsNull)
Steve Naroff4462cb02007-08-16 21:48:38 +00001242 Diag(loc, diag::ext_typecheck_comparison_of_pointer_integer,
1243 lType.getAsString(), rType.getAsString(),
1244 lex->getSourceRange(), rex->getSourceRange());
Chris Lattner22be8422007-08-26 01:10:14 +00001245 promoteExprToType(lex, rType); // promote the integer to pointer
Steve Naroff4462cb02007-08-16 21:48:38 +00001246 return Context.IntTy;
Chris Lattner4b009652007-07-25 00:24:17 +00001247 }
1248 InvalidOperands(loc, lex, rex);
1249 return QualType();
1250}
1251
Chris Lattner4b009652007-07-25 00:24:17 +00001252inline QualType Sema::CheckBitwiseOperands(
Steve Naroff8f708362007-08-24 19:07:16 +00001253 Expr *&lex, Expr *&rex, SourceLocation loc, bool isCompAssign)
Chris Lattner4b009652007-07-25 00:24:17 +00001254{
1255 if (lex->getType()->isVectorType() || rex->getType()->isVectorType())
1256 return CheckVectorOperands(loc, lex, rex);
1257
Steve Naroff8f708362007-08-24 19:07:16 +00001258 QualType compType = UsualArithmeticConversions(lex, rex, isCompAssign);
Chris Lattner4b009652007-07-25 00:24:17 +00001259
1260 if (lex->getType()->isIntegerType() && rex->getType()->isIntegerType())
Steve Naroff8f708362007-08-24 19:07:16 +00001261 return compType;
Chris Lattner4b009652007-07-25 00:24:17 +00001262 InvalidOperands(loc, lex, rex);
1263 return QualType();
1264}
1265
1266inline QualType Sema::CheckLogicalOperands( // C99 6.5.[13,14]
1267 Expr *&lex, Expr *&rex, SourceLocation loc)
1268{
1269 UsualUnaryConversions(lex);
1270 UsualUnaryConversions(rex);
1271
1272 if (lex->getType()->isScalarType() || rex->getType()->isScalarType())
1273 return Context.IntTy;
1274 InvalidOperands(loc, lex, rex);
1275 return QualType();
1276}
1277
1278inline QualType Sema::CheckAssignmentOperands( // C99 6.5.16.1
Steve Naroff0f32f432007-08-24 22:33:52 +00001279 Expr *lex, Expr *&rex, SourceLocation loc, QualType compoundType)
Chris Lattner4b009652007-07-25 00:24:17 +00001280{
1281 QualType lhsType = lex->getType();
1282 QualType rhsType = compoundType.isNull() ? rex->getType() : compoundType;
1283 bool hadError = false;
1284 Expr::isModifiableLvalueResult mlval = lex->isModifiableLvalue();
1285
1286 switch (mlval) { // C99 6.5.16p2
1287 case Expr::MLV_Valid:
1288 break;
1289 case Expr::MLV_ConstQualified:
1290 Diag(loc, diag::err_typecheck_assign_const, lex->getSourceRange());
1291 hadError = true;
1292 break;
1293 case Expr::MLV_ArrayType:
1294 Diag(loc, diag::err_typecheck_array_not_modifiable_lvalue,
1295 lhsType.getAsString(), lex->getSourceRange());
1296 return QualType();
1297 case Expr::MLV_NotObjectType:
1298 Diag(loc, diag::err_typecheck_non_object_not_modifiable_lvalue,
1299 lhsType.getAsString(), lex->getSourceRange());
1300 return QualType();
1301 case Expr::MLV_InvalidExpression:
1302 Diag(loc, diag::err_typecheck_expression_not_modifiable_lvalue,
1303 lex->getSourceRange());
1304 return QualType();
1305 case Expr::MLV_IncompleteType:
1306 case Expr::MLV_IncompleteVoidType:
1307 Diag(loc, diag::err_typecheck_incomplete_type_not_modifiable_lvalue,
1308 lhsType.getAsString(), lex->getSourceRange());
1309 return QualType();
Steve Naroffba67f692007-07-30 03:29:09 +00001310 case Expr::MLV_DuplicateVectorComponents:
1311 Diag(loc, diag::err_typecheck_duplicate_vector_components_not_mlvalue,
1312 lex->getSourceRange());
1313 return QualType();
Chris Lattner4b009652007-07-25 00:24:17 +00001314 }
1315 AssignmentCheckResult result;
1316
1317 if (compoundType.isNull())
1318 result = CheckSingleAssignmentConstraints(lhsType, rex);
1319 else
1320 result = CheckCompoundAssignmentConstraints(lhsType, rhsType);
Steve Naroff7cbb1462007-07-31 12:34:36 +00001321
Chris Lattner4b009652007-07-25 00:24:17 +00001322 // decode the result (notice that extensions still return a type).
1323 switch (result) {
1324 case Compatible:
1325 break;
1326 case Incompatible:
1327 Diag(loc, diag::err_typecheck_assign_incompatible,
1328 lhsType.getAsString(), rhsType.getAsString(),
1329 lex->getSourceRange(), rex->getSourceRange());
1330 hadError = true;
1331 break;
1332 case PointerFromInt:
1333 // check for null pointer constant (C99 6.3.2.3p3)
1334 if (compoundType.isNull() && !rex->isNullPointerConstant(Context)) {
1335 Diag(loc, diag::ext_typecheck_assign_pointer_int,
1336 lhsType.getAsString(), rhsType.getAsString(),
1337 lex->getSourceRange(), rex->getSourceRange());
1338 }
1339 break;
1340 case IntFromPointer:
1341 Diag(loc, diag::ext_typecheck_assign_pointer_int,
1342 lhsType.getAsString(), rhsType.getAsString(),
1343 lex->getSourceRange(), rex->getSourceRange());
1344 break;
1345 case IncompatiblePointer:
1346 Diag(loc, diag::ext_typecheck_assign_incompatible_pointer,
1347 lhsType.getAsString(), rhsType.getAsString(),
1348 lex->getSourceRange(), rex->getSourceRange());
1349 break;
1350 case CompatiblePointerDiscardsQualifiers:
1351 Diag(loc, diag::ext_typecheck_assign_discards_qualifiers,
1352 lhsType.getAsString(), rhsType.getAsString(),
1353 lex->getSourceRange(), rex->getSourceRange());
1354 break;
1355 }
1356 // C99 6.5.16p3: The type of an assignment expression is the type of the
1357 // left operand unless the left operand has qualified type, in which case
1358 // it is the unqualified version of the type of the left operand.
1359 // C99 6.5.16.1p2: In simple assignment, the value of the right operand
1360 // is converted to the type of the assignment expression (above).
Chris Lattner0d9bcea2007-08-30 17:45:32 +00001361 // C++ 5.17p1: the type of the assignment expression is that of its left
1362 // oprdu.
Chris Lattner4b009652007-07-25 00:24:17 +00001363 return hadError ? QualType() : lhsType.getUnqualifiedType();
1364}
1365
1366inline QualType Sema::CheckCommaOperands( // C99 6.5.17
1367 Expr *&lex, Expr *&rex, SourceLocation loc) {
1368 UsualUnaryConversions(rex);
1369 return rex->getType();
1370}
1371
1372/// CheckIncrementDecrementOperand - unlike most "Check" methods, this routine
1373/// doesn't need to call UsualUnaryConversions or UsualArithmeticConversions.
1374QualType Sema::CheckIncrementDecrementOperand(Expr *op, SourceLocation OpLoc) {
1375 QualType resType = op->getType();
1376 assert(!resType.isNull() && "no type for increment/decrement expression");
1377
Steve Naroffd30e1932007-08-24 17:20:07 +00001378 // C99 6.5.2.4p1: We allow complex as a GCC extension.
Chris Lattner4b009652007-07-25 00:24:17 +00001379 if (const PointerType *pt = dyn_cast<PointerType>(resType)) {
1380 if (!pt->getPointeeType()->isObjectType()) { // C99 6.5.2.4p2, 6.5.6p2
1381 Diag(OpLoc, diag::err_typecheck_arithmetic_incomplete_type,
1382 resType.getAsString(), op->getSourceRange());
1383 return QualType();
1384 }
Steve Naroffd30e1932007-08-24 17:20:07 +00001385 } else if (!resType->isRealType()) {
1386 if (resType->isComplexType())
1387 // C99 does not support ++/-- on complex types.
1388 Diag(OpLoc, diag::ext_integer_increment_complex,
1389 resType.getAsString(), op->getSourceRange());
1390 else {
1391 Diag(OpLoc, diag::err_typecheck_illegal_increment_decrement,
1392 resType.getAsString(), op->getSourceRange());
1393 return QualType();
1394 }
Chris Lattner4b009652007-07-25 00:24:17 +00001395 }
Steve Naroff6acc0f42007-08-23 21:37:33 +00001396 // At this point, we know we have a real, complex or pointer type.
1397 // Now make sure the operand is a modifiable lvalue.
Chris Lattner4b009652007-07-25 00:24:17 +00001398 Expr::isModifiableLvalueResult mlval = op->isModifiableLvalue();
1399 if (mlval != Expr::MLV_Valid) {
1400 // FIXME: emit a more precise diagnostic...
1401 Diag(OpLoc, diag::err_typecheck_invalid_lvalue_incr_decr,
1402 op->getSourceRange());
1403 return QualType();
1404 }
1405 return resType;
1406}
1407
1408/// getPrimaryDeclaration - Helper function for CheckAddressOfOperand().
1409/// This routine allows us to typecheck complex/recursive expressions
1410/// where the declaration is needed for type checking. Here are some
1411/// examples: &s.xx, &s.zz[1].yy, &(1+2), &(XX), &"123"[2].
1412static Decl *getPrimaryDeclaration(Expr *e) {
1413 switch (e->getStmtClass()) {
1414 case Stmt::DeclRefExprClass:
1415 return cast<DeclRefExpr>(e)->getDecl();
1416 case Stmt::MemberExprClass:
1417 return getPrimaryDeclaration(cast<MemberExpr>(e)->getBase());
1418 case Stmt::ArraySubscriptExprClass:
1419 return getPrimaryDeclaration(cast<ArraySubscriptExpr>(e)->getBase());
1420 case Stmt::CallExprClass:
1421 return getPrimaryDeclaration(cast<CallExpr>(e)->getCallee());
1422 case Stmt::UnaryOperatorClass:
1423 return getPrimaryDeclaration(cast<UnaryOperator>(e)->getSubExpr());
1424 case Stmt::ParenExprClass:
1425 return getPrimaryDeclaration(cast<ParenExpr>(e)->getSubExpr());
1426 default:
1427 return 0;
1428 }
1429}
1430
1431/// CheckAddressOfOperand - The operand of & must be either a function
1432/// designator or an lvalue designating an object. If it is an lvalue, the
1433/// object cannot be declared with storage class register or be a bit field.
1434/// Note: The usual conversions are *not* applied to the operand of the &
1435/// operator (C99 6.3.2.1p[2-4]), and its result is never an lvalue.
1436QualType Sema::CheckAddressOfOperand(Expr *op, SourceLocation OpLoc) {
1437 Decl *dcl = getPrimaryDeclaration(op);
1438 Expr::isLvalueResult lval = op->isLvalue();
1439
1440 if (lval != Expr::LV_Valid) { // C99 6.5.3.2p1
1441 if (dcl && isa<FunctionDecl>(dcl)) // allow function designators
1442 ;
1443 else { // FIXME: emit more specific diag...
1444 Diag(OpLoc, diag::err_typecheck_invalid_lvalue_addrof,
1445 op->getSourceRange());
1446 return QualType();
1447 }
1448 } else if (dcl) {
1449 // We have an lvalue with a decl. Make sure the decl is not declared
1450 // with the register storage-class specifier.
1451 if (const VarDecl *vd = dyn_cast<VarDecl>(dcl)) {
1452 if (vd->getStorageClass() == VarDecl::Register) {
1453 Diag(OpLoc, diag::err_typecheck_address_of_register,
1454 op->getSourceRange());
1455 return QualType();
1456 }
1457 } else
1458 assert(0 && "Unknown/unexpected decl type");
1459
1460 // FIXME: add check for bitfields!
1461 }
1462 // If the operand has type "type", the result has type "pointer to type".
1463 return Context.getPointerType(op->getType());
1464}
1465
1466QualType Sema::CheckIndirectionOperand(Expr *op, SourceLocation OpLoc) {
1467 UsualUnaryConversions(op);
1468 QualType qType = op->getType();
1469
Chris Lattner7931f4a2007-07-31 16:53:04 +00001470 if (const PointerType *PT = qType->getAsPointerType()) {
Chris Lattner4b009652007-07-25 00:24:17 +00001471 QualType ptype = PT->getPointeeType();
1472 // C99 6.5.3.2p4. "if it points to an object,...".
1473 if (ptype->isIncompleteType()) { // An incomplete type is not an object
1474 // GCC compat: special case 'void *' (treat as warning).
1475 if (ptype->isVoidType()) {
1476 Diag(OpLoc, diag::ext_typecheck_deref_ptr_to_void,
1477 qType.getAsString(), op->getSourceRange());
1478 } else {
1479 Diag(OpLoc, diag::err_typecheck_deref_incomplete_type,
1480 ptype.getAsString(), op->getSourceRange());
1481 return QualType();
1482 }
1483 }
1484 return ptype;
1485 }
1486 Diag(OpLoc, diag::err_typecheck_indirection_requires_pointer,
1487 qType.getAsString(), op->getSourceRange());
1488 return QualType();
1489}
1490
1491static inline BinaryOperator::Opcode ConvertTokenKindToBinaryOpcode(
1492 tok::TokenKind Kind) {
1493 BinaryOperator::Opcode Opc;
1494 switch (Kind) {
1495 default: assert(0 && "Unknown binop!");
1496 case tok::star: Opc = BinaryOperator::Mul; break;
1497 case tok::slash: Opc = BinaryOperator::Div; break;
1498 case tok::percent: Opc = BinaryOperator::Rem; break;
1499 case tok::plus: Opc = BinaryOperator::Add; break;
1500 case tok::minus: Opc = BinaryOperator::Sub; break;
1501 case tok::lessless: Opc = BinaryOperator::Shl; break;
1502 case tok::greatergreater: Opc = BinaryOperator::Shr; break;
1503 case tok::lessequal: Opc = BinaryOperator::LE; break;
1504 case tok::less: Opc = BinaryOperator::LT; break;
1505 case tok::greaterequal: Opc = BinaryOperator::GE; break;
1506 case tok::greater: Opc = BinaryOperator::GT; break;
1507 case tok::exclaimequal: Opc = BinaryOperator::NE; break;
1508 case tok::equalequal: Opc = BinaryOperator::EQ; break;
1509 case tok::amp: Opc = BinaryOperator::And; break;
1510 case tok::caret: Opc = BinaryOperator::Xor; break;
1511 case tok::pipe: Opc = BinaryOperator::Or; break;
1512 case tok::ampamp: Opc = BinaryOperator::LAnd; break;
1513 case tok::pipepipe: Opc = BinaryOperator::LOr; break;
1514 case tok::equal: Opc = BinaryOperator::Assign; break;
1515 case tok::starequal: Opc = BinaryOperator::MulAssign; break;
1516 case tok::slashequal: Opc = BinaryOperator::DivAssign; break;
1517 case tok::percentequal: Opc = BinaryOperator::RemAssign; break;
1518 case tok::plusequal: Opc = BinaryOperator::AddAssign; break;
1519 case tok::minusequal: Opc = BinaryOperator::SubAssign; break;
1520 case tok::lesslessequal: Opc = BinaryOperator::ShlAssign; break;
1521 case tok::greatergreaterequal: Opc = BinaryOperator::ShrAssign; break;
1522 case tok::ampequal: Opc = BinaryOperator::AndAssign; break;
1523 case tok::caretequal: Opc = BinaryOperator::XorAssign; break;
1524 case tok::pipeequal: Opc = BinaryOperator::OrAssign; break;
1525 case tok::comma: Opc = BinaryOperator::Comma; break;
1526 }
1527 return Opc;
1528}
1529
1530static inline UnaryOperator::Opcode ConvertTokenKindToUnaryOpcode(
1531 tok::TokenKind Kind) {
1532 UnaryOperator::Opcode Opc;
1533 switch (Kind) {
1534 default: assert(0 && "Unknown unary op!");
1535 case tok::plusplus: Opc = UnaryOperator::PreInc; break;
1536 case tok::minusminus: Opc = UnaryOperator::PreDec; break;
1537 case tok::amp: Opc = UnaryOperator::AddrOf; break;
1538 case tok::star: Opc = UnaryOperator::Deref; break;
1539 case tok::plus: Opc = UnaryOperator::Plus; break;
1540 case tok::minus: Opc = UnaryOperator::Minus; break;
1541 case tok::tilde: Opc = UnaryOperator::Not; break;
1542 case tok::exclaim: Opc = UnaryOperator::LNot; break;
1543 case tok::kw_sizeof: Opc = UnaryOperator::SizeOf; break;
1544 case tok::kw___alignof: Opc = UnaryOperator::AlignOf; break;
1545 case tok::kw___real: Opc = UnaryOperator::Real; break;
1546 case tok::kw___imag: Opc = UnaryOperator::Imag; break;
1547 case tok::kw___extension__: Opc = UnaryOperator::Extension; break;
1548 }
1549 return Opc;
1550}
1551
1552// Binary Operators. 'Tok' is the token for the operator.
Steve Naroff87d58b42007-09-16 03:34:24 +00001553Action::ExprResult Sema::ActOnBinOp(SourceLocation TokLoc, tok::TokenKind Kind,
Chris Lattner4b009652007-07-25 00:24:17 +00001554 ExprTy *LHS, ExprTy *RHS) {
1555 BinaryOperator::Opcode Opc = ConvertTokenKindToBinaryOpcode(Kind);
1556 Expr *lhs = (Expr *)LHS, *rhs = (Expr*)RHS;
1557
Steve Naroff87d58b42007-09-16 03:34:24 +00001558 assert((lhs != 0) && "ActOnBinOp(): missing left expression");
1559 assert((rhs != 0) && "ActOnBinOp(): missing right expression");
Chris Lattner4b009652007-07-25 00:24:17 +00001560
1561 QualType ResultTy; // Result type of the binary operator.
1562 QualType CompTy; // Computation type for compound assignments (e.g. '+=')
1563
1564 switch (Opc) {
1565 default:
1566 assert(0 && "Unknown binary expr!");
1567 case BinaryOperator::Assign:
1568 ResultTy = CheckAssignmentOperands(lhs, rhs, TokLoc, QualType());
1569 break;
1570 case BinaryOperator::Mul:
1571 case BinaryOperator::Div:
1572 ResultTy = CheckMultiplyDivideOperands(lhs, rhs, TokLoc);
1573 break;
1574 case BinaryOperator::Rem:
1575 ResultTy = CheckRemainderOperands(lhs, rhs, TokLoc);
1576 break;
1577 case BinaryOperator::Add:
1578 ResultTy = CheckAdditionOperands(lhs, rhs, TokLoc);
1579 break;
1580 case BinaryOperator::Sub:
1581 ResultTy = CheckSubtractionOperands(lhs, rhs, TokLoc);
1582 break;
1583 case BinaryOperator::Shl:
1584 case BinaryOperator::Shr:
1585 ResultTy = CheckShiftOperands(lhs, rhs, TokLoc);
1586 break;
1587 case BinaryOperator::LE:
1588 case BinaryOperator::LT:
1589 case BinaryOperator::GE:
1590 case BinaryOperator::GT:
Chris Lattner254f3bc2007-08-26 01:18:55 +00001591 ResultTy = CheckCompareOperands(lhs, rhs, TokLoc, true);
Chris Lattner4b009652007-07-25 00:24:17 +00001592 break;
1593 case BinaryOperator::EQ:
1594 case BinaryOperator::NE:
Chris Lattner254f3bc2007-08-26 01:18:55 +00001595 ResultTy = CheckCompareOperands(lhs, rhs, TokLoc, false);
Chris Lattner4b009652007-07-25 00:24:17 +00001596 break;
1597 case BinaryOperator::And:
1598 case BinaryOperator::Xor:
1599 case BinaryOperator::Or:
1600 ResultTy = CheckBitwiseOperands(lhs, rhs, TokLoc);
1601 break;
1602 case BinaryOperator::LAnd:
1603 case BinaryOperator::LOr:
1604 ResultTy = CheckLogicalOperands(lhs, rhs, TokLoc);
1605 break;
1606 case BinaryOperator::MulAssign:
1607 case BinaryOperator::DivAssign:
Steve Naroff8f708362007-08-24 19:07:16 +00001608 CompTy = CheckMultiplyDivideOperands(lhs, rhs, TokLoc, true);
Chris Lattner4b009652007-07-25 00:24:17 +00001609 if (!CompTy.isNull())
1610 ResultTy = CheckAssignmentOperands(lhs, rhs, TokLoc, CompTy);
1611 break;
1612 case BinaryOperator::RemAssign:
Steve Naroff8f708362007-08-24 19:07:16 +00001613 CompTy = CheckRemainderOperands(lhs, rhs, TokLoc, true);
Chris Lattner4b009652007-07-25 00:24:17 +00001614 if (!CompTy.isNull())
1615 ResultTy = CheckAssignmentOperands(lhs, rhs, TokLoc, CompTy);
1616 break;
1617 case BinaryOperator::AddAssign:
Steve Naroff8f708362007-08-24 19:07:16 +00001618 CompTy = CheckAdditionOperands(lhs, rhs, TokLoc, true);
Chris Lattner4b009652007-07-25 00:24:17 +00001619 if (!CompTy.isNull())
1620 ResultTy = CheckAssignmentOperands(lhs, rhs, TokLoc, CompTy);
1621 break;
1622 case BinaryOperator::SubAssign:
Steve Naroff8f708362007-08-24 19:07:16 +00001623 CompTy = CheckSubtractionOperands(lhs, rhs, TokLoc, true);
Chris Lattner4b009652007-07-25 00:24:17 +00001624 if (!CompTy.isNull())
1625 ResultTy = CheckAssignmentOperands(lhs, rhs, TokLoc, CompTy);
1626 break;
1627 case BinaryOperator::ShlAssign:
1628 case BinaryOperator::ShrAssign:
Steve Naroff8f708362007-08-24 19:07:16 +00001629 CompTy = CheckShiftOperands(lhs, rhs, TokLoc, true);
Chris Lattner4b009652007-07-25 00:24:17 +00001630 if (!CompTy.isNull())
1631 ResultTy = CheckAssignmentOperands(lhs, rhs, TokLoc, CompTy);
1632 break;
1633 case BinaryOperator::AndAssign:
1634 case BinaryOperator::XorAssign:
1635 case BinaryOperator::OrAssign:
Steve Naroff8f708362007-08-24 19:07:16 +00001636 CompTy = CheckBitwiseOperands(lhs, rhs, TokLoc, true);
Chris Lattner4b009652007-07-25 00:24:17 +00001637 if (!CompTy.isNull())
1638 ResultTy = CheckAssignmentOperands(lhs, rhs, TokLoc, CompTy);
1639 break;
1640 case BinaryOperator::Comma:
1641 ResultTy = CheckCommaOperands(lhs, rhs, TokLoc);
1642 break;
1643 }
1644 if (ResultTy.isNull())
1645 return true;
1646 if (CompTy.isNull())
Chris Lattnerf420df12007-08-28 18:36:55 +00001647 return new BinaryOperator(lhs, rhs, Opc, ResultTy, TokLoc);
Chris Lattner4b009652007-07-25 00:24:17 +00001648 else
Chris Lattnerf420df12007-08-28 18:36:55 +00001649 return new CompoundAssignOperator(lhs, rhs, Opc, ResultTy, CompTy, TokLoc);
Chris Lattner4b009652007-07-25 00:24:17 +00001650}
1651
1652// Unary Operators. 'Tok' is the token for the operator.
Steve Naroff87d58b42007-09-16 03:34:24 +00001653Action::ExprResult Sema::ActOnUnaryOp(SourceLocation OpLoc, tok::TokenKind Op,
Chris Lattner4b009652007-07-25 00:24:17 +00001654 ExprTy *input) {
1655 Expr *Input = (Expr*)input;
1656 UnaryOperator::Opcode Opc = ConvertTokenKindToUnaryOpcode(Op);
1657 QualType resultType;
1658 switch (Opc) {
1659 default:
1660 assert(0 && "Unimplemented unary expr!");
1661 case UnaryOperator::PreInc:
1662 case UnaryOperator::PreDec:
1663 resultType = CheckIncrementDecrementOperand(Input, OpLoc);
1664 break;
1665 case UnaryOperator::AddrOf:
1666 resultType = CheckAddressOfOperand(Input, OpLoc);
1667 break;
1668 case UnaryOperator::Deref:
1669 resultType = CheckIndirectionOperand(Input, OpLoc);
1670 break;
1671 case UnaryOperator::Plus:
1672 case UnaryOperator::Minus:
1673 UsualUnaryConversions(Input);
1674 resultType = Input->getType();
1675 if (!resultType->isArithmeticType()) // C99 6.5.3.3p1
1676 return Diag(OpLoc, diag::err_typecheck_unary_expr,
1677 resultType.getAsString());
1678 break;
1679 case UnaryOperator::Not: // bitwise complement
1680 UsualUnaryConversions(Input);
1681 resultType = Input->getType();
Steve Naroffd30e1932007-08-24 17:20:07 +00001682 // C99 6.5.3.3p1. We allow complex as a GCC extension.
1683 if (!resultType->isIntegerType()) {
1684 if (resultType->isComplexType())
1685 // C99 does not support '~' for complex conjugation.
1686 Diag(OpLoc, diag::ext_integer_complement_complex,
1687 resultType.getAsString());
1688 else
1689 return Diag(OpLoc, diag::err_typecheck_unary_expr,
1690 resultType.getAsString());
1691 }
Chris Lattner4b009652007-07-25 00:24:17 +00001692 break;
1693 case UnaryOperator::LNot: // logical negation
1694 // Unlike +/-/~, integer promotions aren't done here (C99 6.5.3.3p5).
1695 DefaultFunctionArrayConversion(Input);
1696 resultType = Input->getType();
1697 if (!resultType->isScalarType()) // C99 6.5.3.3p1
1698 return Diag(OpLoc, diag::err_typecheck_unary_expr,
1699 resultType.getAsString());
1700 // LNot always has type int. C99 6.5.3.3p5.
1701 resultType = Context.IntTy;
1702 break;
1703 case UnaryOperator::SizeOf:
1704 resultType = CheckSizeOfAlignOfOperand(Input->getType(), OpLoc, true);
1705 break;
1706 case UnaryOperator::AlignOf:
1707 resultType = CheckSizeOfAlignOfOperand(Input->getType(), OpLoc, false);
1708 break;
Chris Lattner03931a72007-08-24 21:16:53 +00001709 case UnaryOperator::Real:
Chris Lattner03931a72007-08-24 21:16:53 +00001710 case UnaryOperator::Imag:
Chris Lattner5110ad52007-08-24 21:41:10 +00001711 resultType = CheckRealImagOperand(Input, OpLoc);
Chris Lattner03931a72007-08-24 21:16:53 +00001712 break;
Chris Lattner4b009652007-07-25 00:24:17 +00001713 case UnaryOperator::Extension:
Chris Lattner4b009652007-07-25 00:24:17 +00001714 resultType = Input->getType();
1715 break;
1716 }
1717 if (resultType.isNull())
1718 return true;
1719 return new UnaryOperator(Input, Opc, resultType, OpLoc);
1720}
1721
Steve Naroff5cbb02f2007-09-16 14:56:35 +00001722/// ActOnAddrLabel - Parse the GNU address of label extension: "&&foo".
1723Sema::ExprResult Sema::ActOnAddrLabel(SourceLocation OpLoc,
Chris Lattner4b009652007-07-25 00:24:17 +00001724 SourceLocation LabLoc,
1725 IdentifierInfo *LabelII) {
1726 // Look up the record for this label identifier.
1727 LabelStmt *&LabelDecl = LabelMap[LabelII];
1728
1729 // If we haven't seen this label yet, create a forward reference.
1730 if (LabelDecl == 0)
1731 LabelDecl = new LabelStmt(LabLoc, LabelII, 0);
1732
1733 // Create the AST node. The address of a label always has type 'void*'.
Chris Lattnera0d03a72007-08-03 17:31:20 +00001734 return new AddrLabelExpr(OpLoc, LabLoc, LabelDecl,
1735 Context.getPointerType(Context.VoidTy));
Chris Lattner4b009652007-07-25 00:24:17 +00001736}
1737
Steve Naroff5cbb02f2007-09-16 14:56:35 +00001738Sema::ExprResult Sema::ActOnStmtExpr(SourceLocation LPLoc, StmtTy *substmt,
Chris Lattner4b009652007-07-25 00:24:17 +00001739 SourceLocation RPLoc) { // "({..})"
1740 Stmt *SubStmt = static_cast<Stmt*>(substmt);
1741 assert(SubStmt && isa<CompoundStmt>(SubStmt) && "Invalid action invocation!");
1742 CompoundStmt *Compound = cast<CompoundStmt>(SubStmt);
1743
1744 // FIXME: there are a variety of strange constraints to enforce here, for
1745 // example, it is not possible to goto into a stmt expression apparently.
1746 // More semantic analysis is needed.
1747
1748 // FIXME: the last statement in the compount stmt has its value used. We
1749 // should not warn about it being unused.
1750
1751 // If there are sub stmts in the compound stmt, take the type of the last one
1752 // as the type of the stmtexpr.
1753 QualType Ty = Context.VoidTy;
1754
1755 if (!Compound->body_empty())
1756 if (Expr *LastExpr = dyn_cast<Expr>(Compound->body_back()))
1757 Ty = LastExpr->getType();
1758
1759 return new StmtExpr(Compound, Ty, LPLoc, RPLoc);
1760}
Steve Naroff63bad2d2007-08-01 22:05:33 +00001761
Steve Naroff5cbb02f2007-09-16 14:56:35 +00001762Sema::ExprResult Sema::ActOnBuiltinOffsetOf(SourceLocation BuiltinLoc,
Chris Lattner0d9bcea2007-08-30 17:45:32 +00001763 SourceLocation TypeLoc,
1764 TypeTy *argty,
1765 OffsetOfComponent *CompPtr,
1766 unsigned NumComponents,
1767 SourceLocation RPLoc) {
1768 QualType ArgTy = QualType::getFromOpaquePtr(argty);
1769 assert(!ArgTy.isNull() && "Missing type argument!");
1770
1771 // We must have at least one component that refers to the type, and the first
1772 // one is known to be a field designator. Verify that the ArgTy represents
1773 // a struct/union/class.
1774 if (!ArgTy->isRecordType())
1775 return Diag(TypeLoc, diag::err_offsetof_record_type,ArgTy.getAsString());
1776
1777 // Otherwise, create a compound literal expression as the base, and
1778 // iteratively process the offsetof designators.
1779 Expr *Res = new CompoundLiteralExpr(ArgTy, 0);
1780
Chris Lattnerb37522e2007-08-31 21:49:13 +00001781 // offsetof with non-identifier designators (e.g. "offsetof(x, a.b[c])") are a
1782 // GCC extension, diagnose them.
1783 if (NumComponents != 1)
1784 Diag(BuiltinLoc, diag::ext_offsetof_extended_field_designator,
1785 SourceRange(CompPtr[1].LocStart, CompPtr[NumComponents-1].LocEnd));
1786
Chris Lattner0d9bcea2007-08-30 17:45:32 +00001787 for (unsigned i = 0; i != NumComponents; ++i) {
1788 const OffsetOfComponent &OC = CompPtr[i];
1789 if (OC.isBrackets) {
1790 // Offset of an array sub-field. TODO: Should we allow vector elements?
1791 const ArrayType *AT = Res->getType()->getAsArrayType();
1792 if (!AT) {
1793 delete Res;
1794 return Diag(OC.LocEnd, diag::err_offsetof_array_type,
1795 Res->getType().getAsString());
1796 }
1797
Chris Lattner2af6a802007-08-30 17:59:59 +00001798 // FIXME: C++: Verify that operator[] isn't overloaded.
1799
Chris Lattner0d9bcea2007-08-30 17:45:32 +00001800 // C99 6.5.2.1p1
1801 Expr *Idx = static_cast<Expr*>(OC.U.E);
1802 if (!Idx->getType()->isIntegerType())
1803 return Diag(Idx->getLocStart(), diag::err_typecheck_subscript,
1804 Idx->getSourceRange());
1805
1806 Res = new ArraySubscriptExpr(Res, Idx, AT->getElementType(), OC.LocEnd);
1807 continue;
1808 }
1809
1810 const RecordType *RC = Res->getType()->getAsRecordType();
1811 if (!RC) {
1812 delete Res;
1813 return Diag(OC.LocEnd, diag::err_offsetof_record_type,
1814 Res->getType().getAsString());
1815 }
1816
1817 // Get the decl corresponding to this.
1818 RecordDecl *RD = RC->getDecl();
1819 FieldDecl *MemberDecl = RD->getMember(OC.U.IdentInfo);
1820 if (!MemberDecl)
1821 return Diag(BuiltinLoc, diag::err_typecheck_no_member,
1822 OC.U.IdentInfo->getName(),
1823 SourceRange(OC.LocStart, OC.LocEnd));
Chris Lattner2af6a802007-08-30 17:59:59 +00001824
1825 // FIXME: C++: Verify that MemberDecl isn't a static field.
1826 // FIXME: Verify that MemberDecl isn't a bitfield.
1827
Chris Lattner0d9bcea2007-08-30 17:45:32 +00001828 Res = new MemberExpr(Res, false, MemberDecl, OC.LocEnd);
1829 }
1830
1831 return new UnaryOperator(Res, UnaryOperator::OffsetOf, Context.getSizeType(),
1832 BuiltinLoc);
1833}
1834
1835
Steve Naroff5cbb02f2007-09-16 14:56:35 +00001836Sema::ExprResult Sema::ActOnTypesCompatibleExpr(SourceLocation BuiltinLoc,
Steve Naroff63bad2d2007-08-01 22:05:33 +00001837 TypeTy *arg1, TypeTy *arg2,
1838 SourceLocation RPLoc) {
1839 QualType argT1 = QualType::getFromOpaquePtr(arg1);
1840 QualType argT2 = QualType::getFromOpaquePtr(arg2);
1841
1842 assert((!argT1.isNull() && !argT2.isNull()) && "Missing type argument(s)");
1843
Chris Lattner0d9bcea2007-08-30 17:45:32 +00001844 return new TypesCompatibleExpr(Context.IntTy, BuiltinLoc, argT1, argT2,RPLoc);
Steve Naroff63bad2d2007-08-01 22:05:33 +00001845}
1846
Steve Naroff5cbb02f2007-09-16 14:56:35 +00001847Sema::ExprResult Sema::ActOnChooseExpr(SourceLocation BuiltinLoc, ExprTy *cond,
Steve Naroff93c53012007-08-03 21:21:27 +00001848 ExprTy *expr1, ExprTy *expr2,
1849 SourceLocation RPLoc) {
1850 Expr *CondExpr = static_cast<Expr*>(cond);
1851 Expr *LHSExpr = static_cast<Expr*>(expr1);
1852 Expr *RHSExpr = static_cast<Expr*>(expr2);
1853
1854 assert((CondExpr && LHSExpr && RHSExpr) && "Missing type argument(s)");
1855
1856 // The conditional expression is required to be a constant expression.
1857 llvm::APSInt condEval(32);
1858 SourceLocation ExpLoc;
1859 if (!CondExpr->isIntegerConstantExpr(condEval, Context, &ExpLoc))
1860 return Diag(ExpLoc, diag::err_typecheck_choose_expr_requires_constant,
1861 CondExpr->getSourceRange());
1862
1863 // If the condition is > zero, then the AST type is the same as the LSHExpr.
1864 QualType resType = condEval.getZExtValue() ? LHSExpr->getType() :
1865 RHSExpr->getType();
1866 return new ChooseExpr(BuiltinLoc, CondExpr, LHSExpr, RHSExpr, resType, RPLoc);
1867}
1868
Anders Carlsson36760332007-10-15 20:28:48 +00001869Sema::ExprResult Sema::ActOnVAArg(SourceLocation BuiltinLoc,
1870 ExprTy *expr, TypeTy *type,
1871 SourceLocation RPLoc)
1872{
1873 Expr *E = static_cast<Expr*>(expr);
1874 QualType T = QualType::getFromOpaquePtr(type);
1875
1876 InitBuiltinVaListType();
1877
1878 Sema::AssignmentCheckResult result;
1879
1880 result = CheckAssignmentConstraints(Context.getBuiltinVaListType(),
1881 E->getType());
1882 if (result != Compatible)
1883 return Diag(E->getLocStart(),
1884 diag::err_first_argument_to_va_arg_not_of_type_va_list,
1885 E->getType().getAsString(),
1886 E->getSourceRange());
1887
1888 // FIXME: Warn if a non-POD type is passed in.
1889
1890 return new VAArgExpr(BuiltinLoc, E, T, RPLoc);
1891}
1892
Anders Carlssona66cad42007-08-21 17:43:55 +00001893// TODO: Move this to SemaObjC.cpp
Anders Carlsson8be1d402007-08-22 15:14:15 +00001894Sema::ExprResult Sema::ParseObjCStringLiteral(ExprTy *string) {
Anders Carlssona66cad42007-08-21 17:43:55 +00001895 StringLiteral* S = static_cast<StringLiteral *>(string);
1896
1897 if (CheckBuiltinCFStringArgument(S))
1898 return true;
1899
Steve Narofff2e30312007-10-15 23:35:17 +00001900 if (Context.getObjcConstantStringInterface().isNull()) {
1901 // Initialize the constant string interface lazily. This assumes
1902 // the NSConstantString interface is seen in this translation unit.
1903 IdentifierInfo *NSIdent = &Context.Idents.get("NSConstantString");
1904 ScopedDecl *IFace = LookupScopedDecl(NSIdent, Decl::IDNS_Ordinary,
1905 SourceLocation(), TUScope);
Steve Naroff134c3502007-10-16 00:00:18 +00001906 ObjcInterfaceDecl *strIFace = dyn_cast_or_null<ObjcInterfaceDecl>(IFace);
1907 assert(strIFace && "missing '@interface NSConstantString'");
1908 Context.setObjcConstantStringInterface(strIFace);
Steve Narofff2e30312007-10-15 23:35:17 +00001909 }
1910 QualType t = Context.getObjcConstantStringInterface();
Anders Carlssona66cad42007-08-21 17:43:55 +00001911 t = Context.getPointerType(t);
Anders Carlssona66cad42007-08-21 17:43:55 +00001912 return new ObjCStringLiteral(S, t);
1913}
Anders Carlsson8be1d402007-08-22 15:14:15 +00001914
1915Sema::ExprResult Sema::ParseObjCEncodeExpression(SourceLocation AtLoc,
Chris Lattnercfd61c82007-10-16 22:51:17 +00001916 SourceLocation EncodeLoc,
Anders Carlsson8be1d402007-08-22 15:14:15 +00001917 SourceLocation LParenLoc,
1918 TypeTy *Ty,
1919 SourceLocation RParenLoc) {
1920 QualType EncodedType = QualType::getFromOpaquePtr(Ty);
1921
1922 QualType t = Context.getPointerType(Context.CharTy);
1923 return new ObjCEncodeExpr(t, EncodedType, AtLoc, RParenLoc);
1924}
Steve Naroffd3f5ee42007-09-17 21:01:15 +00001925
Fariborz Jahanianf807c202007-10-16 20:40:23 +00001926Sema::ExprResult Sema::ParseObjCSelectorExpression(Selector Sel,
1927 SourceLocation AtLoc,
Fariborz Jahanian957448a2007-10-16 23:21:02 +00001928 SourceLocation SelLoc,
Fariborz Jahanianf807c202007-10-16 20:40:23 +00001929 SourceLocation LParenLoc,
1930 SourceLocation RParenLoc) {
1931 QualType t = GetObjcSelType(AtLoc);
1932 return new ObjCSelectorExpr(t, Sel, AtLoc, RParenLoc);
1933}
1934
Fariborz Jahanianb391e6e2007-10-17 16:58:11 +00001935Sema::ExprResult Sema::ParseObjCProtocolExpression(IdentifierInfo *ProtocolId,
1936 SourceLocation AtLoc,
1937 SourceLocation ProtoLoc,
1938 SourceLocation LParenLoc,
1939 SourceLocation RParenLoc) {
1940 ObjcProtocolDecl* PDecl = ObjcProtocols[ProtocolId];
1941 if (!PDecl) {
1942 Diag(ProtoLoc, diag::err_undeclared_protocol, ProtocolId->getName());
1943 return true;
1944 }
1945
1946 QualType t = GetObjcProtoType(AtLoc);
1947 return new ObjCProtocolExpr(t, PDecl, AtLoc, RParenLoc);
1948}
Steve Naroff52664182007-10-16 23:12:48 +00001949
1950bool Sema::CheckMessageArgumentTypes(Expr **Args, unsigned NumArgs,
1951 ObjcMethodDecl *Method) {
1952 bool anyIncompatibleArgs = false;
1953
1954 for (unsigned i = 0; i < NumArgs; i++) {
1955 Expr *argExpr = Args[i];
1956 assert(argExpr && "CheckMessageArgumentTypes(): missing expression");
1957
1958 QualType lhsType = Method->getParamDecl(i)->getType();
1959 QualType rhsType = argExpr->getType();
1960
1961 // If necessary, apply function/array conversion. C99 6.7.5.3p[7,8].
1962 if (const ArrayType *ary = lhsType->getAsArrayType())
1963 lhsType = Context.getPointerType(ary->getElementType());
1964 else if (lhsType->isFunctionType())
1965 lhsType = Context.getPointerType(lhsType);
1966
1967 AssignmentCheckResult result = CheckSingleAssignmentConstraints(lhsType,
1968 argExpr);
1969 if (Args[i] != argExpr) // The expression was converted.
1970 Args[i] = argExpr; // Make sure we store the converted expression.
1971 SourceLocation l = argExpr->getLocStart();
1972
1973 // decode the result (notice that AST's are still created for extensions).
1974 switch (result) {
1975 case Compatible:
1976 break;
1977 case PointerFromInt:
1978 // check for null pointer constant (C99 6.3.2.3p3)
1979 if (!argExpr->isNullPointerConstant(Context)) {
1980 Diag(l, diag::ext_typecheck_sending_pointer_int,
1981 lhsType.getAsString(), rhsType.getAsString(),
1982 argExpr->getSourceRange());
1983 }
1984 break;
1985 case IntFromPointer:
1986 Diag(l, diag::ext_typecheck_sending_pointer_int,
1987 lhsType.getAsString(), rhsType.getAsString(),
1988 argExpr->getSourceRange());
1989 break;
1990 case IncompatiblePointer:
1991 Diag(l, diag::ext_typecheck_sending_incompatible_pointer,
1992 rhsType.getAsString(), lhsType.getAsString(),
1993 argExpr->getSourceRange());
1994 break;
1995 case CompatiblePointerDiscardsQualifiers:
1996 Diag(l, diag::ext_typecheck_passing_discards_qualifiers,
1997 rhsType.getAsString(), lhsType.getAsString(),
1998 argExpr->getSourceRange());
1999 break;
2000 case Incompatible:
2001 Diag(l, diag::err_typecheck_sending_incompatible,
2002 rhsType.getAsString(), lhsType.getAsString(),
2003 argExpr->getSourceRange());
2004 anyIncompatibleArgs = true;
2005 }
2006 }
2007 return anyIncompatibleArgs;
2008}
2009
Steve Naroff4ed9d662007-09-27 14:38:14 +00002010// ActOnClassMessage - used for both unary and keyword messages.
2011// ArgExprs is optional - if it is present, the number of expressions
2012// is obtained from Sel.getNumArgs().
2013Sema::ExprResult Sema::ActOnClassMessage(
Steve Narofffa465d12007-10-02 20:01:56 +00002014 IdentifierInfo *receiverName, Selector Sel,
Steve Naroff4ed9d662007-09-27 14:38:14 +00002015 SourceLocation lbrac, SourceLocation rbrac, ExprTy **Args)
Steve Naroffd3f5ee42007-09-17 21:01:15 +00002016{
Steve Narofffa465d12007-10-02 20:01:56 +00002017 assert(receiverName && "missing receiver class name");
Steve Naroffc39ca262007-09-18 23:55:05 +00002018
Steve Naroff52664182007-10-16 23:12:48 +00002019 Expr **ArgExprs = reinterpret_cast<Expr **>(Args);
Steve Narofffa465d12007-10-02 20:01:56 +00002020 ObjcInterfaceDecl* ClassDecl = getObjCInterfaceDecl(receiverName);
2021 ObjcMethodDecl *Method = ClassDecl->lookupClassMethod(Sel);
Steve Naroff7e461452007-10-16 20:39:36 +00002022 QualType returnType;
2023 if (!Method) {
2024 Diag(lbrac, diag::warn_method_not_found, std::string("+"), Sel.getName(),
2025 SourceRange(lbrac, rbrac));
2026 returnType = GetObjcIdType();
2027 } else {
Steve Naroff171f5b12007-10-16 21:36:54 +00002028 returnType = Method->getResultType();
Steve Naroff52664182007-10-16 23:12:48 +00002029 if (Sel.getNumArgs()) {
2030 if (CheckMessageArgumentTypes(ArgExprs, Sel.getNumArgs(), Method))
2031 return true;
2032 }
Steve Naroff7e461452007-10-16 20:39:36 +00002033 }
Steve Naroff7e461452007-10-16 20:39:36 +00002034 return new ObjCMessageExpr(receiverName, Sel, returnType, lbrac, rbrac,
Chris Lattner71c01112007-10-10 23:42:28 +00002035 ArgExprs);
Steve Naroffd3f5ee42007-09-17 21:01:15 +00002036}
2037
Steve Naroff4ed9d662007-09-27 14:38:14 +00002038// ActOnInstanceMessage - used for both unary and keyword messages.
2039// ArgExprs is optional - if it is present, the number of expressions
2040// is obtained from Sel.getNumArgs().
2041Sema::ExprResult Sema::ActOnInstanceMessage(
Steve Naroff6cb1d362007-09-28 22:22:11 +00002042 ExprTy *receiver, Selector Sel,
Steve Naroff4ed9d662007-09-27 14:38:14 +00002043 SourceLocation lbrac, SourceLocation rbrac, ExprTy **Args)
2044{
Steve Naroffc39ca262007-09-18 23:55:05 +00002045 assert(receiver && "missing receiver expression");
2046
Steve Naroff52664182007-10-16 23:12:48 +00002047 Expr **ArgExprs = reinterpret_cast<Expr **>(Args);
Steve Naroffc39ca262007-09-18 23:55:05 +00002048 Expr *RExpr = static_cast<Expr *>(receiver);
Steve Narofffa465d12007-10-02 20:01:56 +00002049 QualType receiverType = RExpr->getType();
Steve Naroffee1de132007-10-10 21:53:07 +00002050 QualType returnType;
2051
2052 if (receiverType == GetObjcIdType()) {
Steve Naroff705380b2007-10-14 23:13:51 +00002053 ObjcMethodDecl *Method = InstanceMethodPool[Sel].Method;
Steve Naroff7e461452007-10-16 20:39:36 +00002054 if (!Method) {
2055 Diag(lbrac, diag::warn_method_not_found, std::string("-"), Sel.getName(),
2056 SourceRange(lbrac, rbrac));
2057 returnType = GetObjcIdType();
2058 } else {
Steve Naroff171f5b12007-10-16 21:36:54 +00002059 returnType = Method->getResultType();
Steve Naroff52664182007-10-16 23:12:48 +00002060 if (Sel.getNumArgs())
2061 if (CheckMessageArgumentTypes(ArgExprs, Sel.getNumArgs(), Method))
2062 return true;
Steve Naroff7e461452007-10-16 20:39:36 +00002063 }
Steve Naroffee1de132007-10-10 21:53:07 +00002064 } else {
Chris Lattner71c01112007-10-10 23:42:28 +00002065 // FIXME (snaroff): checking in this code from Patrick. Needs to be
2066 // revisited. how do we get the ClassDecl from the receiver expression?
Steve Naroffee1de132007-10-10 21:53:07 +00002067 while (receiverType->isPointerType()) {
Chris Lattner71c01112007-10-10 23:42:28 +00002068 PointerType *pointerType =
2069 static_cast<PointerType*>(receiverType.getTypePtr());
Steve Naroffee1de132007-10-10 21:53:07 +00002070 receiverType = pointerType->getPointeeType();
2071 }
Chris Lattner71c01112007-10-10 23:42:28 +00002072 assert(ObjcInterfaceType::classof(receiverType.getTypePtr()) &&
2073 "bad receiver type");
Steve Naroffee1de132007-10-10 21:53:07 +00002074 ObjcInterfaceDecl* ClassDecl = static_cast<ObjcInterfaceType*>(
2075 receiverType.getTypePtr())->getDecl();
Steve Naroff7e461452007-10-16 20:39:36 +00002076 // FIXME: consider using InstanceMethodPool, since it will be faster
2077 // than the following method (which can do *many* linear searches). The
2078 // idea is to add class info to InstanceMethodPool...
Steve Naroffee1de132007-10-10 21:53:07 +00002079 ObjcMethodDecl *Method = ClassDecl->lookupInstanceMethod(Sel);
Steve Naroff7e461452007-10-16 20:39:36 +00002080 if (!Method) {
2081 Diag(lbrac, diag::warn_method_not_found, std::string("-"), Sel.getName(),
2082 SourceRange(lbrac, rbrac));
2083 returnType = GetObjcIdType();
2084 } else {
Steve Naroff171f5b12007-10-16 21:36:54 +00002085 returnType = Method->getResultType();
Steve Naroff52664182007-10-16 23:12:48 +00002086 if (Sel.getNumArgs())
2087 if (CheckMessageArgumentTypes(ArgExprs, Sel.getNumArgs(), Method))
2088 return true;
Steve Naroff7e461452007-10-16 20:39:36 +00002089 }
Steve Narofffa465d12007-10-02 20:01:56 +00002090 }
Steve Narofffa465d12007-10-02 20:01:56 +00002091 return new ObjCMessageExpr(RExpr, Sel, returnType, lbrac, rbrac, ArgExprs);
Steve Naroffd3f5ee42007-09-17 21:01:15 +00002092}