blob: 29f422c04ab5fbf06bde867a048e14728e5cd4e4 [file] [log] [blame]
Reid Spencer5f016e22007-07-11 17:01:13 +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 Naroff6a8a9a42007-10-02 20:01:56 +000017#include "clang/AST/DeclObjC.h"
Reid Spencer5f016e22007-07-11 17:01:13 +000018#include "clang/AST/Expr.h"
Steve Naroff563477d2007-09-18 23:55:05 +000019#include "clang/Parse/DeclSpec.h"
Reid Spencer5f016e22007-07-11 17:01:13 +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 Lattner59907c42007-08-10 20:18:51 +000027#include "llvm/ADT/StringExtras.h"
Reid Spencer5f016e22007-07-11 17:01:13 +000028using namespace clang;
29
Steve Narofff69936d2007-09-16 03:34:24 +000030/// ActOnStringLiteral - The specified tokens were lexed as pasted string
Reid Spencer5f016e22007-07-11 17:01:13 +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 Narofff69936d2007-09-16 03:34:24 +000037Sema::ActOnStringLiteral(const Token *StringToks, unsigned NumStringToks) {
Reid Spencer5f016e22007-07-11 17:01:13 +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
49 QualType t = Context.getPointerType(Context.CharTy);
50
51 // Pass &StringTokLocs[0], StringTokLocs.size() to factory!
52 return new StringLiteral(Literal.GetString(), Literal.GetStringLength(),
53 Literal.AnyWide, t, StringToks[0].getLocation(),
54 StringToks[NumStringToks-1].getLocation());
55}
56
57
Steve Naroff08d92e42007-09-15 18:49:24 +000058/// ActOnIdentifierExpr - The parser read an identifier in expression context,
Reid Spencer5f016e22007-07-11 17:01:13 +000059/// validate it per-C99 6.5.1. HasTrailingLParen indicates whether this
60/// identifier is used in an function call context.
Steve Naroff08d92e42007-09-15 18:49:24 +000061Sema::ExprResult Sema::ActOnIdentifierExpr(Scope *S, SourceLocation Loc,
Reid Spencer5f016e22007-07-11 17:01:13 +000062 IdentifierInfo &II,
63 bool HasTrailingLParen) {
64 // Could be enum-constant or decl.
Steve Naroff8c9f13e2007-09-16 16:16:00 +000065 ScopedDecl *D = LookupScopedDecl(&II, Decl::IDNS_Ordinary, Loc, S);
Reid Spencer5f016e22007-07-11 17:01:13 +000066 if (D == 0) {
67 // Otherwise, this could be an implicitly declared function reference (legal
68 // in C90, extension in C99).
69 if (HasTrailingLParen &&
70 // Not in C++.
71 !getLangOptions().CPlusPlus)
72 D = ImplicitlyDefineFunction(Loc, II, S);
73 else {
74 // If this name wasn't predeclared and if this is not a function call,
75 // diagnose the problem.
76 return Diag(Loc, diag::err_undeclared_var_use, II.getName());
77 }
78 }
Steve Naroffe1223f72007-08-28 03:03:08 +000079 if (ValueDecl *VD = dyn_cast<ValueDecl>(D)) {
Steve Naroff53a32342007-08-28 18:45:29 +000080 // Only create DeclRefExpr's for valid Decl's.
Steve Naroff5912a352007-08-28 20:14:24 +000081 if (VD->isInvalidDecl())
Steve Naroffe1223f72007-08-28 03:03:08 +000082 return true;
Reid Spencer5f016e22007-07-11 17:01:13 +000083 return new DeclRefExpr(VD, VD->getType(), Loc);
Steve Naroffe1223f72007-08-28 03:03:08 +000084 }
Reid Spencer5f016e22007-07-11 17:01:13 +000085 if (isa<TypedefDecl>(D))
86 return Diag(Loc, diag::err_unexpected_typedef, II.getName());
87
88 assert(0 && "Invalid decl");
Chris Lattnereddbe032007-07-21 04:57:45 +000089 abort();
Reid Spencer5f016e22007-07-11 17:01:13 +000090}
91
Steve Narofff69936d2007-09-16 03:34:24 +000092Sema::ExprResult Sema::ActOnPreDefinedExpr(SourceLocation Loc,
Anders Carlsson22742662007-07-21 05:21:51 +000093 tok::TokenKind Kind) {
94 PreDefinedExpr::IdentType IT;
95
Reid Spencer5f016e22007-07-11 17:01:13 +000096 switch (Kind) {
97 default:
98 assert(0 && "Unknown simple primary expr!");
Reid Spencer5f016e22007-07-11 17:01:13 +000099 case tok::kw___func__: // primary-expression: __func__ [C99 6.4.2.2]
Anders Carlsson22742662007-07-21 05:21:51 +0000100 IT = PreDefinedExpr::Func;
101 break;
Reid Spencer5f016e22007-07-11 17:01:13 +0000102 case tok::kw___FUNCTION__: // primary-expression: __FUNCTION__ [GNU]
Anders Carlsson22742662007-07-21 05:21:51 +0000103 IT = PreDefinedExpr::Function;
104 break;
Reid Spencer5f016e22007-07-11 17:01:13 +0000105 case tok::kw___PRETTY_FUNCTION__: // primary-expression: __P..Y_F..N__ [GNU]
Anders Carlsson22742662007-07-21 05:21:51 +0000106 IT = PreDefinedExpr::PrettyFunction;
107 break;
Reid Spencer5f016e22007-07-11 17:01:13 +0000108 }
Anders Carlsson22742662007-07-21 05:21:51 +0000109
110 // Pre-defined identifiers are always of type char *.
111 return new PreDefinedExpr(Loc, Context.getPointerType(Context.CharTy), IT);
Reid Spencer5f016e22007-07-11 17:01:13 +0000112}
113
Steve Narofff69936d2007-09-16 03:34:24 +0000114Sema::ExprResult Sema::ActOnCharacterConstant(const Token &Tok) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000115 llvm::SmallString<16> CharBuffer;
116 CharBuffer.resize(Tok.getLength());
117 const char *ThisTokBegin = &CharBuffer[0];
118 unsigned ActualLength = PP.getSpelling(Tok, ThisTokBegin);
119
120 CharLiteralParser Literal(ThisTokBegin, ThisTokBegin+ActualLength,
121 Tok.getLocation(), PP);
122 if (Literal.hadError())
123 return ExprResult(true);
124 return new CharacterLiteral(Literal.getValue(), Context.IntTy,
125 Tok.getLocation());
126}
127
Steve Narofff69936d2007-09-16 03:34:24 +0000128Action::ExprResult Sema::ActOnNumericConstant(const Token &Tok) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000129 // fast path for a single digit (which is quite common). A single digit
130 // cannot have a trigraph, escaped newline, radix prefix, or type suffix.
131 if (Tok.getLength() == 1) {
132 const char *t = PP.getSourceManager().getCharacterData(Tok.getLocation());
133
Chris Lattner701e5eb2007-09-04 02:45:27 +0000134 unsigned IntSize = static_cast<unsigned>(
135 Context.getTypeSize(Context.IntTy, Tok.getLocation()));
Reid Spencer5f016e22007-07-11 17:01:13 +0000136 return ExprResult(new IntegerLiteral(llvm::APInt(IntSize, *t-'0'),
137 Context.IntTy,
138 Tok.getLocation()));
139 }
140 llvm::SmallString<512> IntegerBuffer;
141 IntegerBuffer.resize(Tok.getLength());
142 const char *ThisTokBegin = &IntegerBuffer[0];
143
144 // Get the spelling of the token, which eliminates trigraphs, etc.
145 unsigned ActualLength = PP.getSpelling(Tok, ThisTokBegin);
146 NumericLiteralParser Literal(ThisTokBegin, ThisTokBegin+ActualLength,
147 Tok.getLocation(), PP);
148 if (Literal.hadError)
149 return ExprResult(true);
150
Chris Lattner5d661452007-08-26 03:42:43 +0000151 Expr *Res;
152
153 if (Literal.isFloatingLiteral()) {
Chris Lattner525a0502007-09-22 18:29:59 +0000154 QualType Ty;
155 const llvm::fltSemantics *Format;
156 uint64_t Size; unsigned Align;
157
158 if (Literal.isFloat) {
159 Ty = Context.FloatTy;
160 Context.Target.getFloatInfo(Size, Align, Format, Tok.getLocation());
161 } else if (Literal.isLong) {
162 Ty = Context.LongDoubleTy;
163 Context.Target.getLongDoubleInfo(Size, Align, Format, Tok.getLocation());
164 } else {
165 Ty = Context.DoubleTy;
166 Context.Target.getDoubleInfo(Size, Align, Format, Tok.getLocation());
167 }
168
169 Res = new FloatingLiteral(Literal.GetFloatValue(*Format), Ty,
170 Tok.getLocation());
Chris Lattner5d661452007-08-26 03:42:43 +0000171 } else if (!Literal.isIntegerLiteral()) {
172 return ExprResult(true);
173 } else {
Reid Spencer5f016e22007-07-11 17:01:13 +0000174 QualType t;
175
Neil Boothb9449512007-08-29 22:00:19 +0000176 // long long is a C99 feature.
177 if (!getLangOptions().C99 && !getLangOptions().CPlusPlus0x &&
Neil Booth79859c32007-08-29 22:13:52 +0000178 Literal.isLongLong)
Neil Boothb9449512007-08-29 22:00:19 +0000179 Diag(Tok.getLocation(), diag::ext_longlong);
180
Reid Spencer5f016e22007-07-11 17:01:13 +0000181 // Get the value in the widest-possible width.
182 llvm::APInt ResultVal(Context.Target.getIntMaxTWidth(Tok.getLocation()), 0);
183
184 if (Literal.GetIntegerValue(ResultVal)) {
185 // If this value didn't fit into uintmax_t, warn and force to ull.
186 Diag(Tok.getLocation(), diag::warn_integer_too_large);
187 t = Context.UnsignedLongLongTy;
Chris Lattnerd2d2a112007-07-14 01:29:45 +0000188 assert(Context.getTypeSize(t, Tok.getLocation()) ==
Reid Spencer5f016e22007-07-11 17:01:13 +0000189 ResultVal.getBitWidth() && "long long is not intmax_t?");
190 } else {
191 // If this value fits into a ULL, try to figure out what else it fits into
192 // according to the rules of C99 6.4.4.1p5.
193
194 // Octal, Hexadecimal, and integers with a U suffix are allowed to
195 // be an unsigned int.
196 bool AllowUnsigned = Literal.isUnsigned || Literal.getRadix() != 10;
197
198 // Check from smallest to largest, picking the smallest type we can.
Chris Lattner97c51562007-08-23 21:58:08 +0000199 if (!Literal.isLong && !Literal.isLongLong) {
200 // Are int/unsigned possibilities?
Chris Lattner701e5eb2007-09-04 02:45:27 +0000201 unsigned IntSize = static_cast<unsigned>(
202 Context.getTypeSize(Context.IntTy,Tok.getLocation()));
Reid Spencer5f016e22007-07-11 17:01:13 +0000203 // Does it fit in a unsigned int?
204 if (ResultVal.isIntN(IntSize)) {
205 // Does it fit in a signed int?
206 if (!Literal.isUnsigned && ResultVal[IntSize-1] == 0)
207 t = Context.IntTy;
208 else if (AllowUnsigned)
209 t = Context.UnsignedIntTy;
210 }
211
212 if (!t.isNull())
213 ResultVal.trunc(IntSize);
214 }
215
216 // Are long/unsigned long possibilities?
217 if (t.isNull() && !Literal.isLongLong) {
Chris Lattner701e5eb2007-09-04 02:45:27 +0000218 unsigned LongSize = static_cast<unsigned>(
219 Context.getTypeSize(Context.LongTy, Tok.getLocation()));
Reid Spencer5f016e22007-07-11 17:01:13 +0000220
221 // Does it fit in a unsigned long?
222 if (ResultVal.isIntN(LongSize)) {
223 // Does it fit in a signed long?
224 if (!Literal.isUnsigned && ResultVal[LongSize-1] == 0)
225 t = Context.LongTy;
226 else if (AllowUnsigned)
227 t = Context.UnsignedLongTy;
228 }
229 if (!t.isNull())
230 ResultVal.trunc(LongSize);
231 }
232
233 // Finally, check long long if needed.
234 if (t.isNull()) {
Chris Lattner701e5eb2007-09-04 02:45:27 +0000235 unsigned LongLongSize = static_cast<unsigned>(
236 Context.getTypeSize(Context.LongLongTy, Tok.getLocation()));
Reid Spencer5f016e22007-07-11 17:01:13 +0000237
238 // Does it fit in a unsigned long long?
239 if (ResultVal.isIntN(LongLongSize)) {
240 // Does it fit in a signed long long?
241 if (!Literal.isUnsigned && ResultVal[LongLongSize-1] == 0)
242 t = Context.LongLongTy;
243 else if (AllowUnsigned)
244 t = Context.UnsignedLongLongTy;
245 }
246 }
247
248 // If we still couldn't decide a type, we probably have something that
249 // does not fit in a signed long long, but has no U suffix.
250 if (t.isNull()) {
251 Diag(Tok.getLocation(), diag::warn_integer_too_large_for_signed);
252 t = Context.UnsignedLongLongTy;
253 }
254 }
255
Chris Lattner5d661452007-08-26 03:42:43 +0000256 Res = new IntegerLiteral(ResultVal, t, Tok.getLocation());
Reid Spencer5f016e22007-07-11 17:01:13 +0000257 }
Chris Lattner5d661452007-08-26 03:42:43 +0000258
259 // If this is an imaginary literal, create the ImaginaryLiteral wrapper.
260 if (Literal.isImaginary)
261 Res = new ImaginaryLiteral(Res, Context.getComplexType(Res->getType()));
262
263 return Res;
Reid Spencer5f016e22007-07-11 17:01:13 +0000264}
265
Steve Narofff69936d2007-09-16 03:34:24 +0000266Action::ExprResult Sema::ActOnParenExpr(SourceLocation L, SourceLocation R,
Reid Spencer5f016e22007-07-11 17:01:13 +0000267 ExprTy *Val) {
268 Expr *e = (Expr *)Val;
Steve Narofff69936d2007-09-16 03:34:24 +0000269 assert((e != 0) && "ActOnParenExpr() missing expr");
Reid Spencer5f016e22007-07-11 17:01:13 +0000270 return new ParenExpr(L, R, e);
271}
272
273/// The UsualUnaryConversions() function is *not* called by this routine.
274/// See C99 6.3.2.1p[2-4] for more details.
275QualType Sema::CheckSizeOfAlignOfOperand(QualType exprType,
276 SourceLocation OpLoc, bool isSizeof) {
277 // C99 6.5.3.4p1:
278 if (isa<FunctionType>(exprType) && isSizeof)
279 // alignof(function) is allowed.
280 Diag(OpLoc, diag::ext_sizeof_function_type);
281 else if (exprType->isVoidType())
282 Diag(OpLoc, diag::ext_sizeof_void_type, isSizeof ? "sizeof" : "__alignof");
283 else if (exprType->isIncompleteType()) {
284 Diag(OpLoc, isSizeof ? diag::err_sizeof_incomplete_type :
285 diag::err_alignof_incomplete_type,
286 exprType.getAsString());
287 return QualType(); // error
288 }
289 // C99 6.5.3.4p4: the type (an unsigned integer type) is size_t.
290 return Context.getSizeType();
291}
292
293Action::ExprResult Sema::
Steve Narofff69936d2007-09-16 03:34:24 +0000294ActOnSizeOfAlignOfTypeExpr(SourceLocation OpLoc, bool isSizeof,
Reid Spencer5f016e22007-07-11 17:01:13 +0000295 SourceLocation LPLoc, TypeTy *Ty,
296 SourceLocation RPLoc) {
297 // If error parsing type, ignore.
298 if (Ty == 0) return true;
299
300 // Verify that this is a valid expression.
301 QualType ArgTy = QualType::getFromOpaquePtr(Ty);
302
303 QualType resultType = CheckSizeOfAlignOfOperand(ArgTy, OpLoc, isSizeof);
304
305 if (resultType.isNull())
306 return true;
307 return new SizeOfAlignOfTypeExpr(isSizeof, ArgTy, resultType, OpLoc, RPLoc);
308}
309
Chris Lattner5d794252007-08-24 21:41:10 +0000310QualType Sema::CheckRealImagOperand(Expr *&V, SourceLocation Loc) {
Chris Lattnerdbb36972007-08-24 21:16:53 +0000311 DefaultFunctionArrayConversion(V);
312
Chris Lattnercc26ed72007-08-26 05:39:26 +0000313 // These operators return the element type of a complex type.
Chris Lattnerdbb36972007-08-24 21:16:53 +0000314 if (const ComplexType *CT = V->getType()->getAsComplexType())
315 return CT->getElementType();
Chris Lattnercc26ed72007-08-26 05:39:26 +0000316
317 // Otherwise they pass through real integer and floating point types here.
318 if (V->getType()->isArithmeticType())
319 return V->getType();
320
321 // Reject anything else.
322 Diag(Loc, diag::err_realimag_invalid_type, V->getType().getAsString());
323 return QualType();
Chris Lattnerdbb36972007-08-24 21:16:53 +0000324}
325
326
Reid Spencer5f016e22007-07-11 17:01:13 +0000327
Steve Narofff69936d2007-09-16 03:34:24 +0000328Action::ExprResult Sema::ActOnPostfixUnaryOp(SourceLocation OpLoc,
Reid Spencer5f016e22007-07-11 17:01:13 +0000329 tok::TokenKind Kind,
330 ExprTy *Input) {
331 UnaryOperator::Opcode Opc;
332 switch (Kind) {
333 default: assert(0 && "Unknown unary op!");
334 case tok::plusplus: Opc = UnaryOperator::PostInc; break;
335 case tok::minusminus: Opc = UnaryOperator::PostDec; break;
336 }
337 QualType result = CheckIncrementDecrementOperand((Expr *)Input, OpLoc);
338 if (result.isNull())
339 return true;
340 return new UnaryOperator((Expr *)Input, Opc, result, OpLoc);
341}
342
343Action::ExprResult Sema::
Steve Narofff69936d2007-09-16 03:34:24 +0000344ActOnArraySubscriptExpr(ExprTy *Base, SourceLocation LLoc,
Reid Spencer5f016e22007-07-11 17:01:13 +0000345 ExprTy *Idx, SourceLocation RLoc) {
Chris Lattner727a80d2007-07-15 23:59:53 +0000346 Expr *LHSExp = static_cast<Expr*>(Base), *RHSExp = static_cast<Expr*>(Idx);
Chris Lattner12d9ff62007-07-16 00:14:47 +0000347
348 // Perform default conversions.
349 DefaultFunctionArrayConversion(LHSExp);
350 DefaultFunctionArrayConversion(RHSExp);
Chris Lattner727a80d2007-07-15 23:59:53 +0000351
Chris Lattner12d9ff62007-07-16 00:14:47 +0000352 QualType LHSTy = LHSExp->getType(), RHSTy = RHSExp->getType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000353
Reid Spencer5f016e22007-07-11 17:01:13 +0000354 // C99 6.5.2.1p2: the expression e1[e2] is by definition precisely equivalent
Chris Lattner73d0d4f2007-08-30 17:45:32 +0000355 // to the expression *((e1)+(e2)). This means the array "Base" may actually be
Reid Spencer5f016e22007-07-11 17:01:13 +0000356 // in the subscript position. As a result, we need to derive the array base
357 // and index from the expression types.
Chris Lattner12d9ff62007-07-16 00:14:47 +0000358 Expr *BaseExpr, *IndexExpr;
359 QualType ResultType;
Chris Lattnerbefee482007-07-31 16:53:04 +0000360 if (const PointerType *PTy = LHSTy->getAsPointerType()) {
Chris Lattner12d9ff62007-07-16 00:14:47 +0000361 BaseExpr = LHSExp;
362 IndexExpr = RHSExp;
363 // FIXME: need to deal with const...
364 ResultType = PTy->getPointeeType();
Chris Lattnerbefee482007-07-31 16:53:04 +0000365 } else if (const PointerType *PTy = RHSTy->getAsPointerType()) {
Chris Lattner7a2e0472007-07-16 00:23:25 +0000366 // Handle the uncommon case of "123[Ptr]".
Chris Lattner12d9ff62007-07-16 00:14:47 +0000367 BaseExpr = RHSExp;
368 IndexExpr = LHSExp;
369 // FIXME: need to deal with const...
370 ResultType = PTy->getPointeeType();
Chris Lattnerc8629632007-07-31 19:29:30 +0000371 } else if (const VectorType *VTy = LHSTy->getAsVectorType()) {
372 BaseExpr = LHSExp; // vectors: V[123]
Chris Lattner12d9ff62007-07-16 00:14:47 +0000373 IndexExpr = RHSExp;
Steve Naroff608e0ee2007-08-03 22:40:33 +0000374
375 // Component access limited to variables (reject vec4.rg[1]).
376 if (!isa<DeclRefExpr>(BaseExpr))
377 return Diag(LLoc, diag::err_ocuvector_component_access,
378 SourceRange(LLoc, RLoc));
Chris Lattner12d9ff62007-07-16 00:14:47 +0000379 // FIXME: need to deal with const...
380 ResultType = VTy->getElementType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000381 } else {
Chris Lattner727a80d2007-07-15 23:59:53 +0000382 return Diag(LHSExp->getLocStart(), diag::err_typecheck_subscript_value,
383 RHSExp->getSourceRange());
Reid Spencer5f016e22007-07-11 17:01:13 +0000384 }
385 // C99 6.5.2.1p1
Chris Lattner12d9ff62007-07-16 00:14:47 +0000386 if (!IndexExpr->getType()->isIntegerType())
387 return Diag(IndexExpr->getLocStart(), diag::err_typecheck_subscript,
388 IndexExpr->getSourceRange());
Reid Spencer5f016e22007-07-11 17:01:13 +0000389
Chris Lattner12d9ff62007-07-16 00:14:47 +0000390 // C99 6.5.2.1p1: "shall have type "pointer to *object* type". In practice,
391 // the following check catches trying to index a pointer to a function (e.g.
392 // void (*)(int)). Functions are not objects in C99.
393 if (!ResultType->isObjectType())
394 return Diag(BaseExpr->getLocStart(),
395 diag::err_typecheck_subscript_not_object,
396 BaseExpr->getType().getAsString(), BaseExpr->getSourceRange());
397
398 return new ArraySubscriptExpr(LHSExp, RHSExp, ResultType, RLoc);
Reid Spencer5f016e22007-07-11 17:01:13 +0000399}
400
Steve Naroffe1b31fe2007-07-27 22:15:19 +0000401QualType Sema::
402CheckOCUVectorComponent(QualType baseType, SourceLocation OpLoc,
403 IdentifierInfo &CompName, SourceLocation CompLoc) {
Chris Lattnerc8629632007-07-31 19:29:30 +0000404 const OCUVectorType *vecType = baseType->getAsOCUVectorType();
Steve Naroffe1b31fe2007-07-27 22:15:19 +0000405
406 // The vector accessor can't exceed the number of elements.
407 const char *compStr = CompName.getName();
408 if (strlen(compStr) > vecType->getNumElements()) {
409 Diag(OpLoc, diag::err_ocuvector_component_exceeds_length,
410 baseType.getAsString(), SourceRange(CompLoc));
411 return QualType();
412 }
413 // The component names must come from the same set.
Chris Lattner88dca042007-08-02 22:33:49 +0000414 if (vecType->getPointAccessorIdx(*compStr) != -1) {
415 do
416 compStr++;
417 while (*compStr && vecType->getPointAccessorIdx(*compStr) != -1);
418 } else if (vecType->getColorAccessorIdx(*compStr) != -1) {
419 do
420 compStr++;
421 while (*compStr && vecType->getColorAccessorIdx(*compStr) != -1);
422 } else if (vecType->getTextureAccessorIdx(*compStr) != -1) {
423 do
424 compStr++;
425 while (*compStr && vecType->getTextureAccessorIdx(*compStr) != -1);
426 }
Steve Naroffe1b31fe2007-07-27 22:15:19 +0000427
428 if (*compStr) {
429 // We didn't get to the end of the string. This means the component names
430 // didn't come from the same set *or* we encountered an illegal name.
431 Diag(OpLoc, diag::err_ocuvector_component_name_illegal,
432 std::string(compStr,compStr+1), SourceRange(CompLoc));
433 return QualType();
434 }
435 // Each component accessor can't exceed the vector type.
436 compStr = CompName.getName();
437 while (*compStr) {
438 if (vecType->isAccessorWithinNumElements(*compStr))
439 compStr++;
440 else
441 break;
442 }
443 if (*compStr) {
444 // We didn't get to the end of the string. This means a component accessor
445 // exceeds the number of elements in the vector.
446 Diag(OpLoc, diag::err_ocuvector_component_exceeds_length,
447 baseType.getAsString(), SourceRange(CompLoc));
448 return QualType();
449 }
450 // The component accessor looks fine - now we need to compute the actual type.
451 // The vector type is implied by the component accessor. For example,
452 // vec4.b is a float, vec4.xy is a vec2, vec4.rgb is a vec3, etc.
453 unsigned CompSize = strlen(CompName.getName());
454 if (CompSize == 1)
455 return vecType->getElementType();
Steve Naroffbea0b342007-07-29 16:33:31 +0000456
457 QualType VT = Context.getOCUVectorType(vecType->getElementType(), CompSize);
458 // Now look up the TypeDefDecl from the vector type. Without this,
459 // diagostics look bad. We want OCU vector types to appear built-in.
460 for (unsigned i = 0, e = OCUVectorDecls.size(); i != e; ++i) {
461 if (OCUVectorDecls[i]->getUnderlyingType() == VT)
462 return Context.getTypedefType(OCUVectorDecls[i]);
463 }
464 return VT; // should never get here (a typedef type should always be found).
Steve Naroffe1b31fe2007-07-27 22:15:19 +0000465}
466
Reid Spencer5f016e22007-07-11 17:01:13 +0000467Action::ExprResult Sema::
Steve Narofff69936d2007-09-16 03:34:24 +0000468ActOnMemberReferenceExpr(ExprTy *Base, SourceLocation OpLoc,
Reid Spencer5f016e22007-07-11 17:01:13 +0000469 tok::TokenKind OpKind, SourceLocation MemberLoc,
470 IdentifierInfo &Member) {
Steve Naroffdfa6aae2007-07-26 03:11:44 +0000471 Expr *BaseExpr = static_cast<Expr *>(Base);
472 assert(BaseExpr && "no record expression");
Reid Spencer5f016e22007-07-11 17:01:13 +0000473
Steve Naroffdfa6aae2007-07-26 03:11:44 +0000474 QualType BaseType = BaseExpr->getType();
475 assert(!BaseType.isNull() && "no type for member expression");
Reid Spencer5f016e22007-07-11 17:01:13 +0000476
Reid Spencer5f016e22007-07-11 17:01:13 +0000477 if (OpKind == tok::arrow) {
Chris Lattnerbefee482007-07-31 16:53:04 +0000478 if (const PointerType *PT = BaseType->getAsPointerType())
Steve Naroffdfa6aae2007-07-26 03:11:44 +0000479 BaseType = PT->getPointeeType();
480 else
481 return Diag(OpLoc, diag::err_typecheck_member_reference_arrow,
482 SourceRange(MemberLoc));
Reid Spencer5f016e22007-07-11 17:01:13 +0000483 }
Steve Naroffe1b31fe2007-07-27 22:15:19 +0000484 // The base type is either a record or an OCUVectorType.
Chris Lattnerc8629632007-07-31 19:29:30 +0000485 if (const RecordType *RTy = BaseType->getAsRecordType()) {
Steve Naroffdfa6aae2007-07-26 03:11:44 +0000486 RecordDecl *RDecl = RTy->getDecl();
487 if (RTy->isIncompleteType())
488 return Diag(OpLoc, diag::err_typecheck_incomplete_tag, RDecl->getName(),
489 BaseExpr->getSourceRange());
490 // The record definition is complete, now make sure the member is valid.
Steve Naroffe1b31fe2007-07-27 22:15:19 +0000491 FieldDecl *MemberDecl = RDecl->getMember(&Member);
492 if (!MemberDecl)
Steve Naroffdfa6aae2007-07-26 03:11:44 +0000493 return Diag(OpLoc, diag::err_typecheck_no_member, Member.getName(),
494 SourceRange(MemberLoc));
Steve Naroffe1b31fe2007-07-27 22:15:19 +0000495 return new MemberExpr(BaseExpr, OpKind==tok::arrow, MemberDecl, MemberLoc);
496 } else if (BaseType->isOCUVectorType() && OpKind == tok::period) {
Steve Naroff608e0ee2007-08-03 22:40:33 +0000497 // Component access limited to variables (reject vec4.rg.g).
498 if (!isa<DeclRefExpr>(BaseExpr))
499 return Diag(OpLoc, diag::err_ocuvector_component_access,
500 SourceRange(MemberLoc));
Steve Naroffe1b31fe2007-07-27 22:15:19 +0000501 QualType ret = CheckOCUVectorComponent(BaseType, OpLoc, Member, MemberLoc);
502 if (ret.isNull())
503 return true;
Chris Lattner6481a572007-08-03 17:31:20 +0000504 return new OCUVectorElementExpr(ret, BaseExpr, Member, MemberLoc);
Steve Naroffdfa6aae2007-07-26 03:11:44 +0000505 } else
506 return Diag(OpLoc, diag::err_typecheck_member_reference_structUnion,
507 SourceRange(MemberLoc));
Reid Spencer5f016e22007-07-11 17:01:13 +0000508}
509
Steve Narofff69936d2007-09-16 03:34:24 +0000510/// ActOnCallExpr - Handle a call to Fn with the specified array of arguments.
Reid Spencer5f016e22007-07-11 17:01:13 +0000511/// This provides the location of the left/right parens and a list of comma
512/// locations.
513Action::ExprResult Sema::
Steve Narofff69936d2007-09-16 03:34:24 +0000514ActOnCallExpr(ExprTy *fn, SourceLocation LParenLoc,
Chris Lattner74c469f2007-07-21 03:03:59 +0000515 ExprTy **args, unsigned NumArgsInCall,
Reid Spencer5f016e22007-07-11 17:01:13 +0000516 SourceLocation *CommaLocs, SourceLocation RParenLoc) {
Chris Lattner74c469f2007-07-21 03:03:59 +0000517 Expr *Fn = static_cast<Expr *>(fn);
518 Expr **Args = reinterpret_cast<Expr**>(args);
519 assert(Fn && "no function call expression");
Reid Spencer5f016e22007-07-11 17:01:13 +0000520
Chris Lattner74c469f2007-07-21 03:03:59 +0000521 UsualUnaryConversions(Fn);
522 QualType funcType = Fn->getType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000523
524 // C99 6.5.2.2p1 - "The expression that denotes the called function shall have
525 // type pointer to function".
Chris Lattner2dcb6bb2007-07-31 21:27:01 +0000526 const PointerType *PT = funcType->getAsPointerType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000527 if (PT == 0)
Chris Lattner74c469f2007-07-21 03:03:59 +0000528 return Diag(Fn->getLocStart(), diag::err_typecheck_call_not_function,
529 SourceRange(Fn->getLocStart(), RParenLoc));
Reid Spencer5f016e22007-07-11 17:01:13 +0000530
Chris Lattner2dcb6bb2007-07-31 21:27:01 +0000531 const FunctionType *funcT = PT->getPointeeType()->getAsFunctionType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000532 if (funcT == 0)
Chris Lattner74c469f2007-07-21 03:03:59 +0000533 return Diag(Fn->getLocStart(), diag::err_typecheck_call_not_function,
534 SourceRange(Fn->getLocStart(), RParenLoc));
Reid Spencer5f016e22007-07-11 17:01:13 +0000535
536 // If a prototype isn't declared, the parser implicitly defines a func decl
537 QualType resultType = funcT->getResultType();
538
539 if (const FunctionTypeProto *proto = dyn_cast<FunctionTypeProto>(funcT)) {
540 // C99 6.5.2.2p7 - the arguments are implicitly converted, as if by
541 // assignment, to the types of the corresponding parameter, ...
542
543 unsigned NumArgsInProto = proto->getNumArgs();
544 unsigned NumArgsToCheck = NumArgsInCall;
545
546 if (NumArgsInCall < NumArgsInProto)
547 Diag(RParenLoc, diag::err_typecheck_call_too_few_args,
Chris Lattner74c469f2007-07-21 03:03:59 +0000548 Fn->getSourceRange());
Reid Spencer5f016e22007-07-11 17:01:13 +0000549 else if (NumArgsInCall > NumArgsInProto) {
550 if (!proto->isVariadic()) {
Chris Lattnerd472b312007-07-21 03:09:58 +0000551 Diag(Args[NumArgsInProto]->getLocStart(),
Chris Lattner74c469f2007-07-21 03:03:59 +0000552 diag::err_typecheck_call_too_many_args, Fn->getSourceRange(),
Chris Lattnerd472b312007-07-21 03:09:58 +0000553 SourceRange(Args[NumArgsInProto]->getLocStart(),
554 Args[NumArgsInCall-1]->getLocEnd()));
Reid Spencer5f016e22007-07-11 17:01:13 +0000555 }
556 NumArgsToCheck = NumArgsInProto;
557 }
558 // Continue to check argument types (even if we have too few/many args).
559 for (unsigned i = 0; i < NumArgsToCheck; i++) {
Chris Lattner74c469f2007-07-21 03:03:59 +0000560 Expr *argExpr = Args[i];
Steve Narofff69936d2007-09-16 03:34:24 +0000561 assert(argExpr && "ActOnCallExpr(): missing argument expression");
Reid Spencer5f016e22007-07-11 17:01:13 +0000562
563 QualType lhsType = proto->getArgType(i);
564 QualType rhsType = argExpr->getType();
Steve Naroff700204c2007-07-24 21:46:40 +0000565
Steve Naroff82c7e6d2007-07-25 20:45:33 +0000566 // If necessary, apply function/array conversion. C99 6.7.5.3p[7,8].
Chris Lattnerc8629632007-07-31 19:29:30 +0000567 if (const ArrayType *ary = lhsType->getAsArrayType())
Steve Naroff700204c2007-07-24 21:46:40 +0000568 lhsType = Context.getPointerType(ary->getElementType());
Steve Naroff82c7e6d2007-07-25 20:45:33 +0000569 else if (lhsType->isFunctionType())
Steve Naroff700204c2007-07-24 21:46:40 +0000570 lhsType = Context.getPointerType(lhsType);
571
Steve Naroff90045e82007-07-13 23:32:42 +0000572 AssignmentCheckResult result = CheckSingleAssignmentConstraints(lhsType,
573 argExpr);
Steve Narofff1120de2007-08-24 22:33:52 +0000574 if (Args[i] != argExpr) // The expression was converted.
575 Args[i] = argExpr; // Make sure we store the converted expression.
Reid Spencer5f016e22007-07-11 17:01:13 +0000576 SourceLocation l = argExpr->getLocStart();
577
578 // decode the result (notice that AST's are still created for extensions).
579 switch (result) {
580 case Compatible:
581 break;
582 case PointerFromInt:
583 // check for null pointer constant (C99 6.3.2.3p3)
Chris Lattner590b6642007-07-15 23:26:56 +0000584 if (!argExpr->isNullPointerConstant(Context)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000585 Diag(l, diag::ext_typecheck_passing_pointer_int,
586 lhsType.getAsString(), rhsType.getAsString(),
Chris Lattner74c469f2007-07-21 03:03:59 +0000587 Fn->getSourceRange(), argExpr->getSourceRange());
Reid Spencer5f016e22007-07-11 17:01:13 +0000588 }
589 break;
590 case IntFromPointer:
591 Diag(l, diag::ext_typecheck_passing_pointer_int,
592 lhsType.getAsString(), rhsType.getAsString(),
Chris Lattner74c469f2007-07-21 03:03:59 +0000593 Fn->getSourceRange(), argExpr->getSourceRange());
Reid Spencer5f016e22007-07-11 17:01:13 +0000594 break;
595 case IncompatiblePointer:
596 Diag(l, diag::ext_typecheck_passing_incompatible_pointer,
597 rhsType.getAsString(), lhsType.getAsString(),
Chris Lattner74c469f2007-07-21 03:03:59 +0000598 Fn->getSourceRange(), argExpr->getSourceRange());
Reid Spencer5f016e22007-07-11 17:01:13 +0000599 break;
600 case CompatiblePointerDiscardsQualifiers:
601 Diag(l, diag::ext_typecheck_passing_discards_qualifiers,
602 rhsType.getAsString(), lhsType.getAsString(),
Chris Lattner74c469f2007-07-21 03:03:59 +0000603 Fn->getSourceRange(), argExpr->getSourceRange());
Reid Spencer5f016e22007-07-11 17:01:13 +0000604 break;
605 case Incompatible:
606 return Diag(l, diag::err_typecheck_passing_incompatible,
607 rhsType.getAsString(), lhsType.getAsString(),
Chris Lattner74c469f2007-07-21 03:03:59 +0000608 Fn->getSourceRange(), argExpr->getSourceRange());
Reid Spencer5f016e22007-07-11 17:01:13 +0000609 }
610 }
Steve Naroffb291ab62007-08-28 23:30:39 +0000611 if (NumArgsInCall > NumArgsInProto && proto->isVariadic()) {
612 // Promote the arguments (C99 6.5.2.2p7).
613 for (unsigned i = NumArgsInProto; i < NumArgsInCall; i++) {
614 Expr *argExpr = Args[i];
Steve Narofff69936d2007-09-16 03:34:24 +0000615 assert(argExpr && "ActOnCallExpr(): missing argument expression");
Steve Naroffb291ab62007-08-28 23:30:39 +0000616
617 DefaultArgumentPromotion(argExpr);
618 if (Args[i] != argExpr) // The expression was converted.
619 Args[i] = argExpr; // Make sure we store the converted expression.
620 }
621 } else if (NumArgsInCall != NumArgsInProto && !proto->isVariadic()) {
622 // Even if the types checked, bail if the number of arguments don't match.
Reid Spencer5f016e22007-07-11 17:01:13 +0000623 return true;
Steve Naroffb291ab62007-08-28 23:30:39 +0000624 }
625 } else if (isa<FunctionTypeNoProto>(funcT)) {
626 // Promote the arguments (C99 6.5.2.2p6).
627 for (unsigned i = 0; i < NumArgsInCall; i++) {
628 Expr *argExpr = Args[i];
Steve Narofff69936d2007-09-16 03:34:24 +0000629 assert(argExpr && "ActOnCallExpr(): missing argument expression");
Steve Naroffb291ab62007-08-28 23:30:39 +0000630
631 DefaultArgumentPromotion(argExpr);
632 if (Args[i] != argExpr) // The expression was converted.
633 Args[i] = argExpr; // Make sure we store the converted expression.
634 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000635 }
Chris Lattner59907c42007-08-10 20:18:51 +0000636 // Do special checking on direct calls to functions.
637 if (ImplicitCastExpr *IcExpr = dyn_cast<ImplicitCastExpr>(Fn))
638 if (DeclRefExpr *DRExpr = dyn_cast<DeclRefExpr>(IcExpr->getSubExpr()))
639 if (FunctionDecl *FDecl = dyn_cast<FunctionDecl>(DRExpr->getDecl()))
Chris Lattner73d0d4f2007-08-30 17:45:32 +0000640 if (CheckFunctionCall(Fn, LParenLoc, RParenLoc, FDecl, Args,
641 NumArgsInCall))
Anders Carlsson71993dd2007-08-17 05:31:46 +0000642 return true;
Chris Lattner59907c42007-08-10 20:18:51 +0000643
Chris Lattner74c469f2007-07-21 03:03:59 +0000644 return new CallExpr(Fn, Args, NumArgsInCall, resultType, RParenLoc);
Reid Spencer5f016e22007-07-11 17:01:13 +0000645}
646
647Action::ExprResult Sema::
Steve Narofff69936d2007-09-16 03:34:24 +0000648ActOnCompoundLiteral(SourceLocation LParenLoc, TypeTy *Ty,
Steve Naroffaff1edd2007-07-19 21:32:11 +0000649 SourceLocation RParenLoc, ExprTy *InitExpr) {
Steve Narofff69936d2007-09-16 03:34:24 +0000650 assert((Ty != 0) && "ActOnCompoundLiteral(): missing type");
Steve Naroff4aa88f82007-07-19 01:06:55 +0000651 QualType literalType = QualType::getFromOpaquePtr(Ty);
Steve Naroffaff1edd2007-07-19 21:32:11 +0000652 // FIXME: put back this assert when initializers are worked out.
Steve Narofff69936d2007-09-16 03:34:24 +0000653 //assert((InitExpr != 0) && "ActOnCompoundLiteral(): missing expression");
Steve Naroffaff1edd2007-07-19 21:32:11 +0000654 Expr *literalExpr = static_cast<Expr*>(InitExpr);
Steve Naroff4aa88f82007-07-19 01:06:55 +0000655
656 // FIXME: add semantic analysis (C99 6.5.2.5).
Steve Naroffaff1edd2007-07-19 21:32:11 +0000657 return new CompoundLiteralExpr(literalType, literalExpr);
Steve Naroff4aa88f82007-07-19 01:06:55 +0000658}
659
660Action::ExprResult Sema::
Steve Narofff69936d2007-09-16 03:34:24 +0000661ActOnInitList(SourceLocation LBraceLoc, ExprTy **initlist, unsigned NumInit,
Anders Carlsson66b5a8a2007-08-31 04:56:16 +0000662 SourceLocation RBraceLoc) {
Steve Narofff0090632007-09-02 02:04:30 +0000663 Expr **InitList = reinterpret_cast<Expr**>(initlist);
Anders Carlsson66b5a8a2007-08-31 04:56:16 +0000664
Steve Naroff08d92e42007-09-15 18:49:24 +0000665 // Semantic analysis for initializers is done by ActOnDeclarator() and
Steve Naroffd35005e2007-09-03 01:24:23 +0000666 // CheckInitializer() - it requires knowledge of the object being intialized.
Anders Carlsson66b5a8a2007-08-31 04:56:16 +0000667
Steve Naroff38374b02007-09-02 20:30:18 +0000668 InitListExpr *e = new InitListExpr(LBraceLoc, InitList, NumInit, RBraceLoc);
669 e->setType(Context.VoidTy); // FIXME: just a place holder for now.
670 return e;
Steve Naroff4aa88f82007-07-19 01:06:55 +0000671}
672
673Action::ExprResult Sema::
Steve Narofff69936d2007-09-16 03:34:24 +0000674ActOnCastExpr(SourceLocation LParenLoc, TypeTy *Ty,
Reid Spencer5f016e22007-07-11 17:01:13 +0000675 SourceLocation RParenLoc, ExprTy *Op) {
Steve Narofff69936d2007-09-16 03:34:24 +0000676 assert((Ty != 0) && (Op != 0) && "ActOnCastExpr(): missing type or expr");
Steve Naroff16beff82007-07-16 23:25:18 +0000677
678 Expr *castExpr = static_cast<Expr*>(Op);
679 QualType castType = QualType::getFromOpaquePtr(Ty);
680
Steve Naroff711602b2007-08-31 00:32:44 +0000681 UsualUnaryConversions(castExpr);
682
Chris Lattner75af4802007-07-18 16:00:06 +0000683 // C99 6.5.4p2: the cast type needs to be void or scalar and the expression
684 // type needs to be scalar.
685 if (!castType->isScalarType() && !castType->isVoidType()) {
Steve Naroff16beff82007-07-16 23:25:18 +0000686 return Diag(LParenLoc, diag::err_typecheck_cond_expect_scalar,
687 castType.getAsString(), SourceRange(LParenLoc, RParenLoc));
688 }
689 if (!castExpr->getType()->isScalarType()) {
690 return Diag(castExpr->getLocStart(),
691 diag::err_typecheck_expect_scalar_operand,
692 castExpr->getType().getAsString(), castExpr->getSourceRange());
693 }
694 return new CastExpr(castType, castExpr, LParenLoc);
Reid Spencer5f016e22007-07-11 17:01:13 +0000695}
696
697inline QualType Sema::CheckConditionalOperands( // C99 6.5.15
Steve Naroff49b45262007-07-13 16:58:59 +0000698 Expr *&cond, Expr *&lex, Expr *&rex, SourceLocation questionLoc) {
Steve Naroffc80b4ee2007-07-16 21:54:35 +0000699 UsualUnaryConversions(cond);
700 UsualUnaryConversions(lex);
701 UsualUnaryConversions(rex);
702 QualType condT = cond->getType();
703 QualType lexT = lex->getType();
704 QualType rexT = rex->getType();
705
Reid Spencer5f016e22007-07-11 17:01:13 +0000706 // first, check the condition.
Steve Naroff49b45262007-07-13 16:58:59 +0000707 if (!condT->isScalarType()) { // C99 6.5.15p2
708 Diag(cond->getLocStart(), diag::err_typecheck_cond_expect_scalar,
709 condT.getAsString());
Reid Spencer5f016e22007-07-11 17:01:13 +0000710 return QualType();
711 }
712 // now check the two expressions.
Steve Naroffa4332e22007-07-17 00:58:39 +0000713 if (lexT->isArithmeticType() && rexT->isArithmeticType()) { // C99 6.5.15p3,5
714 UsualArithmeticConversions(lex, rex);
715 return lex->getType();
716 }
Chris Lattner2dcb6bb2007-07-31 21:27:01 +0000717 if (const RecordType *LHSRT = lexT->getAsRecordType()) { // C99 6.5.15p3
718 if (const RecordType *RHSRT = rexT->getAsRecordType()) {
719
Chris Lattner73d0d4f2007-08-30 17:45:32 +0000720 if (LHSRT->getDecl()->getIdentifier() ==RHSRT->getDecl()->getIdentifier())
Chris Lattner2dcb6bb2007-07-31 21:27:01 +0000721 return lexT;
722
Reid Spencer5f016e22007-07-11 17:01:13 +0000723 Diag(questionLoc, diag::err_typecheck_cond_incompatible_operands,
Steve Naroff49b45262007-07-13 16:58:59 +0000724 lexT.getAsString(), rexT.getAsString(),
725 lex->getSourceRange(), rex->getSourceRange());
Reid Spencer5f016e22007-07-11 17:01:13 +0000726 return QualType();
727 }
728 }
Chris Lattner590b6642007-07-15 23:26:56 +0000729 // C99 6.5.15p3
730 if (lexT->isPointerType() && rex->isNullPointerConstant(Context))
Steve Naroff49b45262007-07-13 16:58:59 +0000731 return lexT;
Chris Lattner590b6642007-07-15 23:26:56 +0000732 if (rexT->isPointerType() && lex->isNullPointerConstant(Context))
Steve Naroff49b45262007-07-13 16:58:59 +0000733 return rexT;
Reid Spencer5f016e22007-07-11 17:01:13 +0000734
Chris Lattner2dcb6bb2007-07-31 21:27:01 +0000735 if (const PointerType *LHSPT = lexT->getAsPointerType()) { // C99 6.5.15p3,6
736 if (const PointerType *RHSPT = rexT->getAsPointerType()) {
737 // get the "pointed to" types
738 QualType lhptee = LHSPT->getPointeeType();
739 QualType rhptee = RHSPT->getPointeeType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000740
Chris Lattner2dcb6bb2007-07-31 21:27:01 +0000741 // ignore qualifiers on void (C99 6.5.15p3, clause 6)
742 if (lhptee->isVoidType() &&
743 (rhptee->isObjectType() || rhptee->isIncompleteType()))
744 return lexT;
745 if (rhptee->isVoidType() &&
746 (lhptee->isObjectType() || lhptee->isIncompleteType()))
747 return rexT;
Reid Spencer5f016e22007-07-11 17:01:13 +0000748
Chris Lattner2dcb6bb2007-07-31 21:27:01 +0000749 if (!Type::typesAreCompatible(lhptee.getUnqualifiedType(),
750 rhptee.getUnqualifiedType())) {
751 Diag(questionLoc, diag::ext_typecheck_cond_incompatible_pointers,
752 lexT.getAsString(), rexT.getAsString(),
753 lex->getSourceRange(), rex->getSourceRange());
754 return lexT; // FIXME: this is an _ext - is this return o.k?
755 }
756 // The pointer types are compatible.
Chris Lattner73d0d4f2007-08-30 17:45:32 +0000757 // C99 6.5.15p6: If both operands are pointers to compatible types *or* to
758 // differently qualified versions of compatible types, the result type is
759 // a pointer to an appropriately qualified version of the *composite*
760 // type.
Chris Lattner2dcb6bb2007-07-31 21:27:01 +0000761 return lexT; // FIXME: Need to return the composite type.
Reid Spencer5f016e22007-07-11 17:01:13 +0000762 }
763 }
Chris Lattner2dcb6bb2007-07-31 21:27:01 +0000764
Steve Naroff49b45262007-07-13 16:58:59 +0000765 if (lexT->isVoidType() && rexT->isVoidType()) // C99 6.5.15p3
766 return lexT;
Reid Spencer5f016e22007-07-11 17:01:13 +0000767
768 Diag(questionLoc, diag::err_typecheck_cond_incompatible_operands,
Steve Naroff49b45262007-07-13 16:58:59 +0000769 lexT.getAsString(), rexT.getAsString(),
770 lex->getSourceRange(), rex->getSourceRange());
Reid Spencer5f016e22007-07-11 17:01:13 +0000771 return QualType();
772}
773
Steve Narofff69936d2007-09-16 03:34:24 +0000774/// ActOnConditionalOp - Parse a ?: operation. Note that 'LHS' may be null
Reid Spencer5f016e22007-07-11 17:01:13 +0000775/// in the case of a the GNU conditional expr extension.
Steve Narofff69936d2007-09-16 03:34:24 +0000776Action::ExprResult Sema::ActOnConditionalOp(SourceLocation QuestionLoc,
Reid Spencer5f016e22007-07-11 17:01:13 +0000777 SourceLocation ColonLoc,
778 ExprTy *Cond, ExprTy *LHS,
779 ExprTy *RHS) {
Chris Lattner26824902007-07-16 21:39:03 +0000780 Expr *CondExpr = (Expr *) Cond;
781 Expr *LHSExpr = (Expr *) LHS, *RHSExpr = (Expr *) RHS;
782 QualType result = CheckConditionalOperands(CondExpr, LHSExpr,
783 RHSExpr, QuestionLoc);
Reid Spencer5f016e22007-07-11 17:01:13 +0000784 if (result.isNull())
785 return true;
Chris Lattner26824902007-07-16 21:39:03 +0000786 return new ConditionalOperator(CondExpr, LHSExpr, RHSExpr, result);
Reid Spencer5f016e22007-07-11 17:01:13 +0000787}
788
Steve Narofffa2eaab2007-07-15 02:02:06 +0000789// promoteExprToType - a helper function to ensure we create exactly one
790// ImplicitCastExpr. As a convenience (to the caller), we return the type.
Steve Naroffa4332e22007-07-17 00:58:39 +0000791static void promoteExprToType(Expr *&expr, QualType type) {
Steve Narofffa2eaab2007-07-15 02:02:06 +0000792 if (ImplicitCastExpr *impCast = dyn_cast<ImplicitCastExpr>(expr))
793 impCast->setType(type);
794 else
795 expr = new ImplicitCastExpr(type, expr);
Steve Naroffa4332e22007-07-17 00:58:39 +0000796 return;
Steve Narofffa2eaab2007-07-15 02:02:06 +0000797}
798
Steve Naroffb291ab62007-08-28 23:30:39 +0000799/// DefaultArgumentPromotion (C99 6.5.2.2p6). Used for function calls that
800/// do not have a prototype. Integer promotions are performed on each
801/// argument, and arguments that have type float are promoted to double.
802void Sema::DefaultArgumentPromotion(Expr *&expr) {
803 QualType t = expr->getType();
804 assert(!t.isNull() && "DefaultArgumentPromotion - missing type");
805
806 if (t->isPromotableIntegerType()) // C99 6.3.1.1p2
807 promoteExprToType(expr, Context.IntTy);
808 if (t == Context.FloatTy)
809 promoteExprToType(expr, Context.DoubleTy);
810}
811
Steve Narofffa2eaab2007-07-15 02:02:06 +0000812/// DefaultFunctionArrayConversion (C99 6.3.2.1p3, C99 6.3.2.1p4).
Steve Naroffc80b4ee2007-07-16 21:54:35 +0000813void Sema::DefaultFunctionArrayConversion(Expr *&e) {
Steve Narofffa2eaab2007-07-15 02:02:06 +0000814 QualType t = e->getType();
Steve Naroff90045e82007-07-13 23:32:42 +0000815 assert(!t.isNull() && "DefaultFunctionArrayConversion - missing type");
Bill Wendling08ad47c2007-07-17 03:52:31 +0000816
Chris Lattnera1d9fde2007-07-31 16:56:34 +0000817 if (const ReferenceType *ref = t->getAsReferenceType()) {
Bill Wendlingea5e79f2007-07-17 04:16:47 +0000818 promoteExprToType(e, ref->getReferenceeType()); // C++ [expr]
819 t = e->getType();
820 }
Steve Narofffa2eaab2007-07-15 02:02:06 +0000821 if (t->isFunctionType())
Steve Naroffc80b4ee2007-07-16 21:54:35 +0000822 promoteExprToType(e, Context.getPointerType(t));
Chris Lattnerc8629632007-07-31 19:29:30 +0000823 else if (const ArrayType *ary = t->getAsArrayType())
Steve Naroffc80b4ee2007-07-16 21:54:35 +0000824 promoteExprToType(e, Context.getPointerType(ary->getElementType()));
Reid Spencer5f016e22007-07-11 17:01:13 +0000825}
826
827/// UsualUnaryConversion - Performs various conversions that are common to most
828/// operators (C99 6.3). The conversions of array and function types are
829/// sometimes surpressed. For example, the array->pointer conversion doesn't
830/// apply if the array is an argument to the sizeof or address (&) operators.
831/// In these instances, this routine should *not* be called.
Steve Naroffc80b4ee2007-07-16 21:54:35 +0000832void Sema::UsualUnaryConversions(Expr *&expr) {
Steve Naroff49b45262007-07-13 16:58:59 +0000833 QualType t = expr->getType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000834 assert(!t.isNull() && "UsualUnaryConversions - missing type");
835
Chris Lattnera1d9fde2007-07-31 16:56:34 +0000836 if (const ReferenceType *ref = t->getAsReferenceType()) {
Bill Wendlingea5e79f2007-07-17 04:16:47 +0000837 promoteExprToType(expr, ref->getReferenceeType()); // C++ [expr]
838 t = expr->getType();
839 }
Steve Narofffa2eaab2007-07-15 02:02:06 +0000840 if (t->isPromotableIntegerType()) // C99 6.3.1.1p2
Steve Naroffc80b4ee2007-07-16 21:54:35 +0000841 promoteExprToType(expr, Context.IntTy);
842 else
843 DefaultFunctionArrayConversion(expr);
Reid Spencer5f016e22007-07-11 17:01:13 +0000844}
845
Chris Lattner73d0d4f2007-08-30 17:45:32 +0000846/// UsualArithmeticConversions - Performs various conversions that are common to
Reid Spencer5f016e22007-07-11 17:01:13 +0000847/// binary operators (C99 6.3.1.8). If both operands aren't arithmetic, this
848/// routine returns the first non-arithmetic type found. The client is
849/// responsible for emitting appropriate error diagnostics.
Steve Naroff9f5fa9b2007-08-24 19:07:16 +0000850QualType Sema::UsualArithmeticConversions(Expr *&lhsExpr, Expr *&rhsExpr,
851 bool isCompAssign) {
Steve Naroff8702a0f2007-08-25 19:54:59 +0000852 if (!isCompAssign) {
853 UsualUnaryConversions(lhsExpr);
854 UsualUnaryConversions(rhsExpr);
855 }
Steve Naroff3e5e5562007-07-16 22:23:01 +0000856 QualType lhs = lhsExpr->getType();
857 QualType rhs = rhsExpr->getType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000858
859 // If both types are identical, no conversion is needed.
860 if (lhs == rhs)
Steve Naroff9f5fa9b2007-08-24 19:07:16 +0000861 return lhs;
Reid Spencer5f016e22007-07-11 17:01:13 +0000862
863 // If either side is a non-arithmetic type (e.g. a pointer), we are done.
864 // The caller can deal with this (e.g. pointer + int).
Steve Naroffa4332e22007-07-17 00:58:39 +0000865 if (!lhs->isArithmeticType() || !rhs->isArithmeticType())
Steve Naroff9f5fa9b2007-08-24 19:07:16 +0000866 return lhs;
Reid Spencer5f016e22007-07-11 17:01:13 +0000867
868 // At this point, we have two different arithmetic types.
869
870 // Handle complex types first (C99 6.3.1.8p1).
871 if (lhs->isComplexType() || rhs->isComplexType()) {
872 // if we have an integer operand, the result is the complex type.
Steve Naroffa4332e22007-07-17 00:58:39 +0000873 if (rhs->isIntegerType()) { // convert the rhs to the lhs complex type.
Steve Naroff9f5fa9b2007-08-24 19:07:16 +0000874 if (!isCompAssign) promoteExprToType(rhsExpr, lhs);
875 return lhs;
Steve Naroffa4332e22007-07-17 00:58:39 +0000876 }
877 if (lhs->isIntegerType()) { // convert the lhs to the rhs complex type.
Steve Naroff9f5fa9b2007-08-24 19:07:16 +0000878 if (!isCompAssign) promoteExprToType(lhsExpr, rhs);
879 return rhs;
Steve Naroffa4332e22007-07-17 00:58:39 +0000880 }
Steve Narofff1448a02007-08-27 01:27:54 +0000881 // This handles complex/complex, complex/float, or float/complex.
882 // When both operands are complex, the shorter operand is converted to the
883 // type of the longer, and that is the type of the result. This corresponds
884 // to what is done when combining two real floating-point operands.
885 // The fun begins when size promotion occur across type domains.
886 // From H&S 6.3.4: When one operand is complex and the other is a real
887 // floating-point type, the less precise type is converted, within it's
888 // real or complex domain, to the precision of the other type. For example,
889 // when combining a "long double" with a "double _Complex", the
890 // "double _Complex" is promoted to "long double _Complex".
Steve Narofffb0d4962007-08-27 15:30:22 +0000891 int result = Context.compareFloatingType(lhs, rhs);
892
893 if (result > 0) { // The left side is bigger, convert rhs.
Steve Naroff55fe4552007-08-27 21:32:55 +0000894 rhs = Context.getFloatingTypeOfSizeWithinDomain(lhs, rhs);
895 if (!isCompAssign)
896 promoteExprToType(rhsExpr, rhs);
897 } else if (result < 0) { // The right side is bigger, convert lhs.
898 lhs = Context.getFloatingTypeOfSizeWithinDomain(rhs, lhs);
899 if (!isCompAssign)
900 promoteExprToType(lhsExpr, lhs);
901 }
902 // At this point, lhs and rhs have the same rank/size. Now, make sure the
903 // domains match. This is a requirement for our implementation, C99
904 // does not require this promotion.
905 if (lhs != rhs) { // Domains don't match, we have complex/float mix.
906 if (lhs->isRealFloatingType()) { // handle "double, _Complex double".
Steve Naroff29960362007-08-27 21:43:43 +0000907 if (!isCompAssign)
908 promoteExprToType(lhsExpr, rhs);
909 return rhs;
Steve Naroff55fe4552007-08-27 21:32:55 +0000910 } else { // handle "_Complex double, double".
Steve Naroff29960362007-08-27 21:43:43 +0000911 if (!isCompAssign)
912 promoteExprToType(rhsExpr, lhs);
913 return lhs;
Steve Naroff55fe4552007-08-27 21:32:55 +0000914 }
Steve Naroffa4332e22007-07-17 00:58:39 +0000915 }
Steve Naroff29960362007-08-27 21:43:43 +0000916 return lhs; // The domain/size match exactly.
Reid Spencer5f016e22007-07-11 17:01:13 +0000917 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000918 // Now handle "real" floating types (i.e. float, double, long double).
919 if (lhs->isRealFloatingType() || rhs->isRealFloatingType()) {
920 // if we have an integer operand, the result is the real floating type.
Steve Naroffa4332e22007-07-17 00:58:39 +0000921 if (rhs->isIntegerType()) { // convert rhs to the lhs floating point type.
Steve Naroff9f5fa9b2007-08-24 19:07:16 +0000922 if (!isCompAssign) promoteExprToType(rhsExpr, lhs);
923 return lhs;
Steve Naroffa4332e22007-07-17 00:58:39 +0000924 }
925 if (lhs->isIntegerType()) { // convert lhs to the rhs floating point type.
Steve Naroff9f5fa9b2007-08-24 19:07:16 +0000926 if (!isCompAssign) promoteExprToType(lhsExpr, rhs);
927 return rhs;
Steve Naroffa4332e22007-07-17 00:58:39 +0000928 }
Steve Narofffa2eaab2007-07-15 02:02:06 +0000929 // We have two real floating types, float/complex combos were handled above.
930 // Convert the smaller operand to the bigger result.
Steve Narofffb0d4962007-08-27 15:30:22 +0000931 int result = Context.compareFloatingType(lhs, rhs);
932
933 if (result > 0) { // convert the rhs
Steve Naroff9f5fa9b2007-08-24 19:07:16 +0000934 if (!isCompAssign) promoteExprToType(rhsExpr, lhs);
935 return lhs;
Steve Naroffa4332e22007-07-17 00:58:39 +0000936 }
Steve Narofffb0d4962007-08-27 15:30:22 +0000937 if (result < 0) { // convert the lhs
938 if (!isCompAssign) promoteExprToType(lhsExpr, rhs); // convert the lhs
939 return rhs;
940 }
941 assert(0 && "Sema::UsualArithmeticConversions(): illegal float comparison");
Reid Spencer5f016e22007-07-11 17:01:13 +0000942 }
Steve Narofffa2eaab2007-07-15 02:02:06 +0000943 // Finally, we have two differing integer types.
Steve Naroffa4332e22007-07-17 00:58:39 +0000944 if (Context.maxIntegerType(lhs, rhs) == lhs) { // convert the rhs
Steve Naroff9f5fa9b2007-08-24 19:07:16 +0000945 if (!isCompAssign) promoteExprToType(rhsExpr, lhs);
946 return lhs;
Steve Naroffa4332e22007-07-17 00:58:39 +0000947 }
Steve Naroff9f5fa9b2007-08-24 19:07:16 +0000948 if (!isCompAssign) promoteExprToType(lhsExpr, rhs); // convert the lhs
949 return rhs;
Reid Spencer5f016e22007-07-11 17:01:13 +0000950}
951
952// CheckPointerTypesForAssignment - This is a very tricky routine (despite
953// being closely modeled after the C99 spec:-). The odd characteristic of this
954// routine is it effectively iqnores the qualifiers on the top level pointee.
955// This circumvents the usual type rules specified in 6.2.7p1 & 6.7.5.[1-3].
956// FIXME: add a couple examples in this comment.
957Sema::AssignmentCheckResult
958Sema::CheckPointerTypesForAssignment(QualType lhsType, QualType rhsType) {
959 QualType lhptee, rhptee;
960
961 // get the "pointed to" type (ignoring qualifiers at the top level)
Chris Lattner2dcb6bb2007-07-31 21:27:01 +0000962 lhptee = lhsType->getAsPointerType()->getPointeeType();
963 rhptee = rhsType->getAsPointerType()->getPointeeType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000964
965 // make sure we operate on the canonical type
966 lhptee = lhptee.getCanonicalType();
967 rhptee = rhptee.getCanonicalType();
968
969 AssignmentCheckResult r = Compatible;
970
971 // C99 6.5.16.1p1: This following citation is common to constraints
972 // 3 & 4 (below). ...and the type *pointed to* by the left has all the
973 // qualifiers of the type *pointed to* by the right;
974 if ((lhptee.getQualifiers() & rhptee.getQualifiers()) !=
975 rhptee.getQualifiers())
976 r = CompatiblePointerDiscardsQualifiers;
977
978 // C99 6.5.16.1p1 (constraint 4): If one operand is a pointer to an object or
979 // incomplete type and the other is a pointer to a qualified or unqualified
980 // version of void...
981 if (lhptee.getUnqualifiedType()->isVoidType() &&
982 (rhptee->isObjectType() || rhptee->isIncompleteType()))
983 ;
984 else if (rhptee.getUnqualifiedType()->isVoidType() &&
985 (lhptee->isObjectType() || lhptee->isIncompleteType()))
986 ;
987 // C99 6.5.16.1p1 (constraint 3): both operands are pointers to qualified or
988 // unqualified versions of compatible types, ...
989 else if (!Type::typesAreCompatible(lhptee.getUnqualifiedType(),
990 rhptee.getUnqualifiedType()))
991 r = IncompatiblePointer; // this "trumps" PointerAssignDiscardsQualifiers
992 return r;
993}
994
995/// CheckAssignmentConstraints (C99 6.5.16) - This routine currently
996/// has code to accommodate several GCC extensions when type checking
997/// pointers. Here are some objectionable examples that GCC considers warnings:
998///
999/// int a, *pint;
1000/// short *pshort;
1001/// struct foo *pfoo;
1002///
1003/// pint = pshort; // warning: assignment from incompatible pointer type
1004/// a = pint; // warning: assignment makes integer from pointer without a cast
1005/// pint = a; // warning: assignment makes pointer from integer without a cast
1006/// pint = pfoo; // warning: assignment from incompatible pointer type
1007///
1008/// As a result, the code for dealing with pointers is more complex than the
1009/// C99 spec dictates.
1010/// Note: the warning above turn into errors when -pedantic-errors is enabled.
1011///
1012Sema::AssignmentCheckResult
1013Sema::CheckAssignmentConstraints(QualType lhsType, QualType rhsType) {
Steve Naroff700204c2007-07-24 21:46:40 +00001014 if (lhsType == rhsType) // common case, fast path...
1015 return Compatible;
1016
Reid Spencer5f016e22007-07-11 17:01:13 +00001017 if (lhsType->isArithmeticType() && rhsType->isArithmeticType()) {
1018 if (lhsType->isVectorType() || rhsType->isVectorType()) {
1019 if (lhsType.getCanonicalType() != rhsType.getCanonicalType())
1020 return Incompatible;
1021 }
1022 return Compatible;
1023 } else if (lhsType->isPointerType()) {
1024 if (rhsType->isIntegerType())
1025 return PointerFromInt;
1026
1027 if (rhsType->isPointerType())
1028 return CheckPointerTypesForAssignment(lhsType, rhsType);
1029 } else if (rhsType->isPointerType()) {
1030 // C99 6.5.16.1p1: the left operand is _Bool and the right is a pointer.
1031 if ((lhsType->isIntegerType()) && (lhsType != Context.BoolTy))
1032 return IntFromPointer;
1033
1034 if (lhsType->isPointerType())
1035 return CheckPointerTypesForAssignment(lhsType, rhsType);
1036 } else if (isa<TagType>(lhsType) && isa<TagType>(rhsType)) {
1037 if (Type::tagTypesAreCompatible(lhsType, rhsType))
1038 return Compatible;
1039 } else if (lhsType->isReferenceType() || rhsType->isReferenceType()) {
1040 if (Type::referenceTypesAreCompatible(lhsType, rhsType))
1041 return Compatible;
1042 }
1043 return Incompatible;
1044}
1045
Steve Naroff90045e82007-07-13 23:32:42 +00001046Sema::AssignmentCheckResult
1047Sema::CheckSingleAssignmentConstraints(QualType lhsType, Expr *&rExpr) {
1048 // This check seems unnatural, however it is necessary to insure the proper
1049 // conversion of functions/arrays. If the conversion were done for all
Steve Naroff08d92e42007-09-15 18:49:24 +00001050 // DeclExpr's (created by ActOnIdentifierExpr), it would mess up the unary
Steve Naroff90045e82007-07-13 23:32:42 +00001051 // expressions that surpress this implicit conversion (&, sizeof).
Steve Naroffc80b4ee2007-07-16 21:54:35 +00001052 DefaultFunctionArrayConversion(rExpr);
Steve Narofff1120de2007-08-24 22:33:52 +00001053
1054 Sema::AssignmentCheckResult result;
Steve Naroff90045e82007-07-13 23:32:42 +00001055
Steve Narofff1120de2007-08-24 22:33:52 +00001056 result = CheckAssignmentConstraints(lhsType, rExpr->getType());
1057
1058 // C99 6.5.16.1p2: The value of the right operand is converted to the
1059 // type of the assignment expression.
1060 if (rExpr->getType() != lhsType)
1061 promoteExprToType(rExpr, lhsType);
1062 return result;
Steve Naroff90045e82007-07-13 23:32:42 +00001063}
1064
1065Sema::AssignmentCheckResult
1066Sema::CheckCompoundAssignmentConstraints(QualType lhsType, QualType rhsType) {
1067 return CheckAssignmentConstraints(lhsType, rhsType);
1068}
1069
Steve Naroff49b45262007-07-13 16:58:59 +00001070inline void Sema::InvalidOperands(SourceLocation loc, Expr *&lex, Expr *&rex) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001071 Diag(loc, diag::err_typecheck_invalid_operands,
1072 lex->getType().getAsString(), rex->getType().getAsString(),
1073 lex->getSourceRange(), rex->getSourceRange());
1074}
1075
Steve Naroff49b45262007-07-13 16:58:59 +00001076inline QualType Sema::CheckVectorOperands(SourceLocation loc, Expr *&lex,
1077 Expr *&rex) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001078 QualType lhsType = lex->getType(), rhsType = rex->getType();
1079
1080 // make sure the vector types are identical.
1081 if (lhsType == rhsType)
1082 return lhsType;
1083 // You cannot convert between vector values of different size.
1084 Diag(loc, diag::err_typecheck_vector_not_convertable,
1085 lex->getType().getAsString(), rex->getType().getAsString(),
1086 lex->getSourceRange(), rex->getSourceRange());
1087 return QualType();
1088}
1089
1090inline QualType Sema::CheckMultiplyDivideOperands(
Steve Naroff9f5fa9b2007-08-24 19:07:16 +00001091 Expr *&lex, Expr *&rex, SourceLocation loc, bool isCompAssign)
Reid Spencer5f016e22007-07-11 17:01:13 +00001092{
Steve Naroff90045e82007-07-13 23:32:42 +00001093 QualType lhsType = lex->getType(), rhsType = rex->getType();
1094
1095 if (lhsType->isVectorType() || rhsType->isVectorType())
Reid Spencer5f016e22007-07-11 17:01:13 +00001096 return CheckVectorOperands(loc, lex, rex);
Steve Naroff49b45262007-07-13 16:58:59 +00001097
Steve Naroff9f5fa9b2007-08-24 19:07:16 +00001098 QualType compType = UsualArithmeticConversions(lex, rex, isCompAssign);
Reid Spencer5f016e22007-07-11 17:01:13 +00001099
Steve Naroffa4332e22007-07-17 00:58:39 +00001100 if (lex->getType()->isArithmeticType() && rex->getType()->isArithmeticType())
Steve Naroff9f5fa9b2007-08-24 19:07:16 +00001101 return compType;
Reid Spencer5f016e22007-07-11 17:01:13 +00001102 InvalidOperands(loc, lex, rex);
1103 return QualType();
1104}
1105
1106inline QualType Sema::CheckRemainderOperands(
Steve Naroff9f5fa9b2007-08-24 19:07:16 +00001107 Expr *&lex, Expr *&rex, SourceLocation loc, bool isCompAssign)
Reid Spencer5f016e22007-07-11 17:01:13 +00001108{
Steve Naroff90045e82007-07-13 23:32:42 +00001109 QualType lhsType = lex->getType(), rhsType = rex->getType();
1110
Steve Naroff9f5fa9b2007-08-24 19:07:16 +00001111 QualType compType = UsualArithmeticConversions(lex, rex, isCompAssign);
Reid Spencer5f016e22007-07-11 17:01:13 +00001112
Steve Naroffa4332e22007-07-17 00:58:39 +00001113 if (lex->getType()->isIntegerType() && rex->getType()->isIntegerType())
Steve Naroff9f5fa9b2007-08-24 19:07:16 +00001114 return compType;
Reid Spencer5f016e22007-07-11 17:01:13 +00001115 InvalidOperands(loc, lex, rex);
1116 return QualType();
1117}
1118
1119inline QualType Sema::CheckAdditionOperands( // C99 6.5.6
Steve Naroff9f5fa9b2007-08-24 19:07:16 +00001120 Expr *&lex, Expr *&rex, SourceLocation loc, bool isCompAssign)
Reid Spencer5f016e22007-07-11 17:01:13 +00001121{
Steve Naroff3e5e5562007-07-16 22:23:01 +00001122 if (lex->getType()->isVectorType() || rex->getType()->isVectorType())
Steve Naroff49b45262007-07-13 16:58:59 +00001123 return CheckVectorOperands(loc, lex, rex);
1124
Steve Naroff9f5fa9b2007-08-24 19:07:16 +00001125 QualType compType = UsualArithmeticConversions(lex, rex, isCompAssign);
Steve Naroff3e5e5562007-07-16 22:23:01 +00001126
Reid Spencer5f016e22007-07-11 17:01:13 +00001127 // handle the common case first (both operands are arithmetic).
Steve Naroffa4332e22007-07-17 00:58:39 +00001128 if (lex->getType()->isArithmeticType() && rex->getType()->isArithmeticType())
Steve Naroff9f5fa9b2007-08-24 19:07:16 +00001129 return compType;
Reid Spencer5f016e22007-07-11 17:01:13 +00001130
Steve Naroffa4332e22007-07-17 00:58:39 +00001131 if (lex->getType()->isPointerType() && rex->getType()->isIntegerType())
1132 return lex->getType();
1133 if (lex->getType()->isIntegerType() && rex->getType()->isPointerType())
1134 return rex->getType();
Reid Spencer5f016e22007-07-11 17:01:13 +00001135 InvalidOperands(loc, lex, rex);
1136 return QualType();
1137}
1138
1139inline QualType Sema::CheckSubtractionOperands( // C99 6.5.6
Steve Naroff9f5fa9b2007-08-24 19:07:16 +00001140 Expr *&lex, Expr *&rex, SourceLocation loc, bool isCompAssign)
Reid Spencer5f016e22007-07-11 17:01:13 +00001141{
Steve Naroff3e5e5562007-07-16 22:23:01 +00001142 if (lex->getType()->isVectorType() || rex->getType()->isVectorType())
Reid Spencer5f016e22007-07-11 17:01:13 +00001143 return CheckVectorOperands(loc, lex, rex);
Steve Naroff90045e82007-07-13 23:32:42 +00001144
Steve Naroff9f5fa9b2007-08-24 19:07:16 +00001145 QualType compType = UsualArithmeticConversions(lex, rex, isCompAssign);
Reid Spencer5f016e22007-07-11 17:01:13 +00001146
1147 // handle the common case first (both operands are arithmetic).
Steve Naroffa4332e22007-07-17 00:58:39 +00001148 if (lex->getType()->isArithmeticType() && rex->getType()->isArithmeticType())
Steve Naroff9f5fa9b2007-08-24 19:07:16 +00001149 return compType;
Steve Naroff3e5e5562007-07-16 22:23:01 +00001150
1151 if (lex->getType()->isPointerType() && rex->getType()->isIntegerType())
Steve Naroff9f5fa9b2007-08-24 19:07:16 +00001152 return compType;
Steve Naroff3e5e5562007-07-16 22:23:01 +00001153 if (lex->getType()->isPointerType() && rex->getType()->isPointerType())
Chris Lattner8b9023b2007-07-13 03:05:23 +00001154 return Context.getPointerDiffType();
Reid Spencer5f016e22007-07-11 17:01:13 +00001155 InvalidOperands(loc, lex, rex);
1156 return QualType();
1157}
1158
1159inline QualType Sema::CheckShiftOperands( // C99 6.5.7
Steve Naroff9f5fa9b2007-08-24 19:07:16 +00001160 Expr *&lex, Expr *&rex, SourceLocation loc, bool isCompAssign)
Reid Spencer5f016e22007-07-11 17:01:13 +00001161{
1162 // FIXME: Shifts don't perform usual arithmetic conversions. This is wrong
1163 // for int << longlong -> the result type should be int, not long long.
Steve Naroff9f5fa9b2007-08-24 19:07:16 +00001164 QualType compType = UsualArithmeticConversions(lex, rex, isCompAssign);
Reid Spencer5f016e22007-07-11 17:01:13 +00001165
Steve Naroffa4332e22007-07-17 00:58:39 +00001166 // handle the common case first (both operands are arithmetic).
1167 if (lex->getType()->isIntegerType() && rex->getType()->isIntegerType())
Steve Naroff9f5fa9b2007-08-24 19:07:16 +00001168 return compType;
Reid Spencer5f016e22007-07-11 17:01:13 +00001169 InvalidOperands(loc, lex, rex);
1170 return QualType();
1171}
1172
Chris Lattnera5937dd2007-08-26 01:18:55 +00001173inline QualType Sema::CheckCompareOperands( // C99 6.5.8
1174 Expr *&lex, Expr *&rex, SourceLocation loc, bool isRelational)
Reid Spencer5f016e22007-07-11 17:01:13 +00001175{
Chris Lattnera5937dd2007-08-26 01:18:55 +00001176 // C99 6.5.8p3 / C99 6.5.9p4
Steve Naroff30bf7712007-08-10 18:26:40 +00001177 if (lex->getType()->isArithmeticType() && rex->getType()->isArithmeticType())
1178 UsualArithmeticConversions(lex, rex);
1179 else {
1180 UsualUnaryConversions(lex);
1181 UsualUnaryConversions(rex);
1182 }
Steve Naroffc80b4ee2007-07-16 21:54:35 +00001183 QualType lType = lex->getType();
1184 QualType rType = rex->getType();
Reid Spencer5f016e22007-07-11 17:01:13 +00001185
Chris Lattnera5937dd2007-08-26 01:18:55 +00001186 if (isRelational) {
1187 if (lType->isRealType() && rType->isRealType())
1188 return Context.IntTy;
1189 } else {
Chris Lattner915311c2007-08-30 06:10:41 +00001190 if (lType->isFloatingType() && rType->isFloatingType())
Ted Kremenek9b3d3a92007-08-29 18:06:12 +00001191 Diag(loc, diag::warn_floatingpoint_eq);
1192
Chris Lattnera5937dd2007-08-26 01:18:55 +00001193 if (lType->isArithmeticType() && rType->isArithmeticType())
1194 return Context.IntTy;
1195 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001196
Chris Lattnerd28f8152007-08-26 01:10:14 +00001197 bool LHSIsNull = lex->isNullPointerConstant(Context);
1198 bool RHSIsNull = rex->isNullPointerConstant(Context);
1199
Chris Lattnera5937dd2007-08-26 01:18:55 +00001200 // All of the following pointer related warnings are GCC extensions, except
1201 // when handling null pointer constants. One day, we can consider making them
1202 // errors (when -pedantic-errors is enabled).
Steve Naroff77878cc2007-08-27 04:08:11 +00001203 if (lType->isPointerType() && rType->isPointerType()) { // C99 6.5.8p2
Chris Lattnerd28f8152007-08-26 01:10:14 +00001204 if (!LHSIsNull && !RHSIsNull &&
Steve Naroff77878cc2007-08-27 04:08:11 +00001205 !Type::pointerTypesAreCompatible(lType.getUnqualifiedType(),
1206 rType.getUnqualifiedType())) {
Steve Naroffe77fd3c2007-08-16 21:48:38 +00001207 Diag(loc, diag::ext_typecheck_comparison_of_distinct_pointers,
1208 lType.getAsString(), rType.getAsString(),
1209 lex->getSourceRange(), rex->getSourceRange());
Reid Spencer5f016e22007-07-11 17:01:13 +00001210 }
Chris Lattnerd28f8152007-08-26 01:10:14 +00001211 promoteExprToType(rex, lType); // promote the pointer to pointer
Steve Naroffe77fd3c2007-08-16 21:48:38 +00001212 return Context.IntTy;
1213 }
1214 if (lType->isPointerType() && rType->isIntegerType()) {
Chris Lattnerd28f8152007-08-26 01:10:14 +00001215 if (!RHSIsNull)
Steve Naroffe77fd3c2007-08-16 21:48:38 +00001216 Diag(loc, diag::ext_typecheck_comparison_of_pointer_integer,
1217 lType.getAsString(), rType.getAsString(),
1218 lex->getSourceRange(), rex->getSourceRange());
Chris Lattnerd28f8152007-08-26 01:10:14 +00001219 promoteExprToType(rex, lType); // promote the integer to pointer
Steve Naroffe77fd3c2007-08-16 21:48:38 +00001220 return Context.IntTy;
1221 }
1222 if (lType->isIntegerType() && rType->isPointerType()) {
Chris Lattnerd28f8152007-08-26 01:10:14 +00001223 if (!LHSIsNull)
Steve Naroffe77fd3c2007-08-16 21:48:38 +00001224 Diag(loc, diag::ext_typecheck_comparison_of_pointer_integer,
1225 lType.getAsString(), rType.getAsString(),
1226 lex->getSourceRange(), rex->getSourceRange());
Chris Lattnerd28f8152007-08-26 01:10:14 +00001227 promoteExprToType(lex, rType); // promote the integer to pointer
Steve Naroffe77fd3c2007-08-16 21:48:38 +00001228 return Context.IntTy;
Reid Spencer5f016e22007-07-11 17:01:13 +00001229 }
1230 InvalidOperands(loc, lex, rex);
1231 return QualType();
1232}
1233
Reid Spencer5f016e22007-07-11 17:01:13 +00001234inline QualType Sema::CheckBitwiseOperands(
Steve Naroff9f5fa9b2007-08-24 19:07:16 +00001235 Expr *&lex, Expr *&rex, SourceLocation loc, bool isCompAssign)
Reid Spencer5f016e22007-07-11 17:01:13 +00001236{
Steve Naroff3e5e5562007-07-16 22:23:01 +00001237 if (lex->getType()->isVectorType() || rex->getType()->isVectorType())
Reid Spencer5f016e22007-07-11 17:01:13 +00001238 return CheckVectorOperands(loc, lex, rex);
Steve Naroff90045e82007-07-13 23:32:42 +00001239
Steve Naroff9f5fa9b2007-08-24 19:07:16 +00001240 QualType compType = UsualArithmeticConversions(lex, rex, isCompAssign);
Reid Spencer5f016e22007-07-11 17:01:13 +00001241
Steve Naroffa4332e22007-07-17 00:58:39 +00001242 if (lex->getType()->isIntegerType() && rex->getType()->isIntegerType())
Steve Naroff9f5fa9b2007-08-24 19:07:16 +00001243 return compType;
Reid Spencer5f016e22007-07-11 17:01:13 +00001244 InvalidOperands(loc, lex, rex);
1245 return QualType();
1246}
1247
1248inline QualType Sema::CheckLogicalOperands( // C99 6.5.[13,14]
Steve Naroff49b45262007-07-13 16:58:59 +00001249 Expr *&lex, Expr *&rex, SourceLocation loc)
Reid Spencer5f016e22007-07-11 17:01:13 +00001250{
Steve Naroffc80b4ee2007-07-16 21:54:35 +00001251 UsualUnaryConversions(lex);
1252 UsualUnaryConversions(rex);
Reid Spencer5f016e22007-07-11 17:01:13 +00001253
Steve Naroffa4332e22007-07-17 00:58:39 +00001254 if (lex->getType()->isScalarType() || rex->getType()->isScalarType())
Reid Spencer5f016e22007-07-11 17:01:13 +00001255 return Context.IntTy;
1256 InvalidOperands(loc, lex, rex);
1257 return QualType();
1258}
1259
1260inline QualType Sema::CheckAssignmentOperands( // C99 6.5.16.1
Steve Narofff1120de2007-08-24 22:33:52 +00001261 Expr *lex, Expr *&rex, SourceLocation loc, QualType compoundType)
Reid Spencer5f016e22007-07-11 17:01:13 +00001262{
1263 QualType lhsType = lex->getType();
1264 QualType rhsType = compoundType.isNull() ? rex->getType() : compoundType;
1265 bool hadError = false;
1266 Expr::isModifiableLvalueResult mlval = lex->isModifiableLvalue();
1267
1268 switch (mlval) { // C99 6.5.16p2
1269 case Expr::MLV_Valid:
1270 break;
1271 case Expr::MLV_ConstQualified:
1272 Diag(loc, diag::err_typecheck_assign_const, lex->getSourceRange());
1273 hadError = true;
1274 break;
1275 case Expr::MLV_ArrayType:
1276 Diag(loc, diag::err_typecheck_array_not_modifiable_lvalue,
1277 lhsType.getAsString(), lex->getSourceRange());
1278 return QualType();
1279 case Expr::MLV_NotObjectType:
1280 Diag(loc, diag::err_typecheck_non_object_not_modifiable_lvalue,
1281 lhsType.getAsString(), lex->getSourceRange());
1282 return QualType();
1283 case Expr::MLV_InvalidExpression:
1284 Diag(loc, diag::err_typecheck_expression_not_modifiable_lvalue,
1285 lex->getSourceRange());
1286 return QualType();
1287 case Expr::MLV_IncompleteType:
1288 case Expr::MLV_IncompleteVoidType:
1289 Diag(loc, diag::err_typecheck_incomplete_type_not_modifiable_lvalue,
1290 lhsType.getAsString(), lex->getSourceRange());
1291 return QualType();
Steve Narofffec0b492007-07-30 03:29:09 +00001292 case Expr::MLV_DuplicateVectorComponents:
1293 Diag(loc, diag::err_typecheck_duplicate_vector_components_not_mlvalue,
1294 lex->getSourceRange());
1295 return QualType();
Reid Spencer5f016e22007-07-11 17:01:13 +00001296 }
Steve Naroff90045e82007-07-13 23:32:42 +00001297 AssignmentCheckResult result;
1298
1299 if (compoundType.isNull())
1300 result = CheckSingleAssignmentConstraints(lhsType, rex);
1301 else
1302 result = CheckCompoundAssignmentConstraints(lhsType, rhsType);
Steve Naroffd1861fd2007-07-31 12:34:36 +00001303
Reid Spencer5f016e22007-07-11 17:01:13 +00001304 // decode the result (notice that extensions still return a type).
1305 switch (result) {
1306 case Compatible:
1307 break;
1308 case Incompatible:
1309 Diag(loc, diag::err_typecheck_assign_incompatible,
1310 lhsType.getAsString(), rhsType.getAsString(),
1311 lex->getSourceRange(), rex->getSourceRange());
1312 hadError = true;
1313 break;
1314 case PointerFromInt:
1315 // check for null pointer constant (C99 6.3.2.3p3)
Chris Lattner590b6642007-07-15 23:26:56 +00001316 if (compoundType.isNull() && !rex->isNullPointerConstant(Context)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001317 Diag(loc, diag::ext_typecheck_assign_pointer_int,
1318 lhsType.getAsString(), rhsType.getAsString(),
1319 lex->getSourceRange(), rex->getSourceRange());
1320 }
1321 break;
1322 case IntFromPointer:
1323 Diag(loc, diag::ext_typecheck_assign_pointer_int,
1324 lhsType.getAsString(), rhsType.getAsString(),
1325 lex->getSourceRange(), rex->getSourceRange());
1326 break;
1327 case IncompatiblePointer:
1328 Diag(loc, diag::ext_typecheck_assign_incompatible_pointer,
1329 lhsType.getAsString(), rhsType.getAsString(),
1330 lex->getSourceRange(), rex->getSourceRange());
1331 break;
1332 case CompatiblePointerDiscardsQualifiers:
1333 Diag(loc, diag::ext_typecheck_assign_discards_qualifiers,
1334 lhsType.getAsString(), rhsType.getAsString(),
1335 lex->getSourceRange(), rex->getSourceRange());
1336 break;
1337 }
1338 // C99 6.5.16p3: The type of an assignment expression is the type of the
1339 // left operand unless the left operand has qualified type, in which case
1340 // it is the unqualified version of the type of the left operand.
1341 // C99 6.5.16.1p2: In simple assignment, the value of the right operand
1342 // is converted to the type of the assignment expression (above).
Chris Lattner73d0d4f2007-08-30 17:45:32 +00001343 // C++ 5.17p1: the type of the assignment expression is that of its left
1344 // oprdu.
Reid Spencer5f016e22007-07-11 17:01:13 +00001345 return hadError ? QualType() : lhsType.getUnqualifiedType();
1346}
1347
1348inline QualType Sema::CheckCommaOperands( // C99 6.5.17
Steve Naroff49b45262007-07-13 16:58:59 +00001349 Expr *&lex, Expr *&rex, SourceLocation loc) {
Steve Naroffc80b4ee2007-07-16 21:54:35 +00001350 UsualUnaryConversions(rex);
1351 return rex->getType();
Reid Spencer5f016e22007-07-11 17:01:13 +00001352}
1353
Steve Naroff49b45262007-07-13 16:58:59 +00001354/// CheckIncrementDecrementOperand - unlike most "Check" methods, this routine
1355/// doesn't need to call UsualUnaryConversions or UsualArithmeticConversions.
Reid Spencer5f016e22007-07-11 17:01:13 +00001356QualType Sema::CheckIncrementDecrementOperand(Expr *op, SourceLocation OpLoc) {
Steve Naroff49b45262007-07-13 16:58:59 +00001357 QualType resType = op->getType();
Reid Spencer5f016e22007-07-11 17:01:13 +00001358 assert(!resType.isNull() && "no type for increment/decrement expression");
1359
Steve Naroff084f9ed2007-08-24 17:20:07 +00001360 // C99 6.5.2.4p1: We allow complex as a GCC extension.
Reid Spencer5f016e22007-07-11 17:01:13 +00001361 if (const PointerType *pt = dyn_cast<PointerType>(resType)) {
1362 if (!pt->getPointeeType()->isObjectType()) { // C99 6.5.2.4p2, 6.5.6p2
1363 Diag(OpLoc, diag::err_typecheck_arithmetic_incomplete_type,
1364 resType.getAsString(), op->getSourceRange());
1365 return QualType();
1366 }
Steve Naroff084f9ed2007-08-24 17:20:07 +00001367 } else if (!resType->isRealType()) {
1368 if (resType->isComplexType())
1369 // C99 does not support ++/-- on complex types.
1370 Diag(OpLoc, diag::ext_integer_increment_complex,
1371 resType.getAsString(), op->getSourceRange());
1372 else {
1373 Diag(OpLoc, diag::err_typecheck_illegal_increment_decrement,
1374 resType.getAsString(), op->getSourceRange());
1375 return QualType();
1376 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001377 }
Steve Naroffdd10e022007-08-23 21:37:33 +00001378 // At this point, we know we have a real, complex or pointer type.
1379 // Now make sure the operand is a modifiable lvalue.
Reid Spencer5f016e22007-07-11 17:01:13 +00001380 Expr::isModifiableLvalueResult mlval = op->isModifiableLvalue();
1381 if (mlval != Expr::MLV_Valid) {
1382 // FIXME: emit a more precise diagnostic...
1383 Diag(OpLoc, diag::err_typecheck_invalid_lvalue_incr_decr,
1384 op->getSourceRange());
1385 return QualType();
1386 }
1387 return resType;
1388}
1389
1390/// getPrimaryDeclaration - Helper function for CheckAddressOfOperand().
1391/// This routine allows us to typecheck complex/recursive expressions
1392/// where the declaration is needed for type checking. Here are some
1393/// examples: &s.xx, &s.zz[1].yy, &(1+2), &(XX), &"123"[2].
1394static Decl *getPrimaryDeclaration(Expr *e) {
1395 switch (e->getStmtClass()) {
1396 case Stmt::DeclRefExprClass:
1397 return cast<DeclRefExpr>(e)->getDecl();
1398 case Stmt::MemberExprClass:
1399 return getPrimaryDeclaration(cast<MemberExpr>(e)->getBase());
1400 case Stmt::ArraySubscriptExprClass:
1401 return getPrimaryDeclaration(cast<ArraySubscriptExpr>(e)->getBase());
1402 case Stmt::CallExprClass:
1403 return getPrimaryDeclaration(cast<CallExpr>(e)->getCallee());
1404 case Stmt::UnaryOperatorClass:
1405 return getPrimaryDeclaration(cast<UnaryOperator>(e)->getSubExpr());
1406 case Stmt::ParenExprClass:
1407 return getPrimaryDeclaration(cast<ParenExpr>(e)->getSubExpr());
1408 default:
1409 return 0;
1410 }
1411}
1412
1413/// CheckAddressOfOperand - The operand of & must be either a function
1414/// designator or an lvalue designating an object. If it is an lvalue, the
1415/// object cannot be declared with storage class register or be a bit field.
1416/// Note: The usual conversions are *not* applied to the operand of the &
1417/// operator (C99 6.3.2.1p[2-4]), and its result is never an lvalue.
1418QualType Sema::CheckAddressOfOperand(Expr *op, SourceLocation OpLoc) {
1419 Decl *dcl = getPrimaryDeclaration(op);
1420 Expr::isLvalueResult lval = op->isLvalue();
1421
1422 if (lval != Expr::LV_Valid) { // C99 6.5.3.2p1
1423 if (dcl && isa<FunctionDecl>(dcl)) // allow function designators
1424 ;
1425 else { // FIXME: emit more specific diag...
1426 Diag(OpLoc, diag::err_typecheck_invalid_lvalue_addrof,
1427 op->getSourceRange());
1428 return QualType();
1429 }
1430 } else if (dcl) {
1431 // We have an lvalue with a decl. Make sure the decl is not declared
1432 // with the register storage-class specifier.
1433 if (const VarDecl *vd = dyn_cast<VarDecl>(dcl)) {
1434 if (vd->getStorageClass() == VarDecl::Register) {
1435 Diag(OpLoc, diag::err_typecheck_address_of_register,
1436 op->getSourceRange());
1437 return QualType();
1438 }
1439 } else
1440 assert(0 && "Unknown/unexpected decl type");
1441
1442 // FIXME: add check for bitfields!
1443 }
1444 // If the operand has type "type", the result has type "pointer to type".
1445 return Context.getPointerType(op->getType());
1446}
1447
1448QualType Sema::CheckIndirectionOperand(Expr *op, SourceLocation OpLoc) {
Steve Naroffc80b4ee2007-07-16 21:54:35 +00001449 UsualUnaryConversions(op);
1450 QualType qType = op->getType();
Reid Spencer5f016e22007-07-11 17:01:13 +00001451
Chris Lattnerbefee482007-07-31 16:53:04 +00001452 if (const PointerType *PT = qType->getAsPointerType()) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001453 QualType ptype = PT->getPointeeType();
1454 // C99 6.5.3.2p4. "if it points to an object,...".
1455 if (ptype->isIncompleteType()) { // An incomplete type is not an object
1456 // GCC compat: special case 'void *' (treat as warning).
1457 if (ptype->isVoidType()) {
1458 Diag(OpLoc, diag::ext_typecheck_deref_ptr_to_void,
1459 qType.getAsString(), op->getSourceRange());
1460 } else {
1461 Diag(OpLoc, diag::err_typecheck_deref_incomplete_type,
1462 ptype.getAsString(), op->getSourceRange());
1463 return QualType();
1464 }
1465 }
1466 return ptype;
1467 }
1468 Diag(OpLoc, diag::err_typecheck_indirection_requires_pointer,
1469 qType.getAsString(), op->getSourceRange());
1470 return QualType();
1471}
1472
1473static inline BinaryOperator::Opcode ConvertTokenKindToBinaryOpcode(
1474 tok::TokenKind Kind) {
1475 BinaryOperator::Opcode Opc;
1476 switch (Kind) {
1477 default: assert(0 && "Unknown binop!");
1478 case tok::star: Opc = BinaryOperator::Mul; break;
1479 case tok::slash: Opc = BinaryOperator::Div; break;
1480 case tok::percent: Opc = BinaryOperator::Rem; break;
1481 case tok::plus: Opc = BinaryOperator::Add; break;
1482 case tok::minus: Opc = BinaryOperator::Sub; break;
1483 case tok::lessless: Opc = BinaryOperator::Shl; break;
1484 case tok::greatergreater: Opc = BinaryOperator::Shr; break;
1485 case tok::lessequal: Opc = BinaryOperator::LE; break;
1486 case tok::less: Opc = BinaryOperator::LT; break;
1487 case tok::greaterequal: Opc = BinaryOperator::GE; break;
1488 case tok::greater: Opc = BinaryOperator::GT; break;
1489 case tok::exclaimequal: Opc = BinaryOperator::NE; break;
1490 case tok::equalequal: Opc = BinaryOperator::EQ; break;
1491 case tok::amp: Opc = BinaryOperator::And; break;
1492 case tok::caret: Opc = BinaryOperator::Xor; break;
1493 case tok::pipe: Opc = BinaryOperator::Or; break;
1494 case tok::ampamp: Opc = BinaryOperator::LAnd; break;
1495 case tok::pipepipe: Opc = BinaryOperator::LOr; break;
1496 case tok::equal: Opc = BinaryOperator::Assign; break;
1497 case tok::starequal: Opc = BinaryOperator::MulAssign; break;
1498 case tok::slashequal: Opc = BinaryOperator::DivAssign; break;
1499 case tok::percentequal: Opc = BinaryOperator::RemAssign; break;
1500 case tok::plusequal: Opc = BinaryOperator::AddAssign; break;
1501 case tok::minusequal: Opc = BinaryOperator::SubAssign; break;
1502 case tok::lesslessequal: Opc = BinaryOperator::ShlAssign; break;
1503 case tok::greatergreaterequal: Opc = BinaryOperator::ShrAssign; break;
1504 case tok::ampequal: Opc = BinaryOperator::AndAssign; break;
1505 case tok::caretequal: Opc = BinaryOperator::XorAssign; break;
1506 case tok::pipeequal: Opc = BinaryOperator::OrAssign; break;
1507 case tok::comma: Opc = BinaryOperator::Comma; break;
1508 }
1509 return Opc;
1510}
1511
1512static inline UnaryOperator::Opcode ConvertTokenKindToUnaryOpcode(
1513 tok::TokenKind Kind) {
1514 UnaryOperator::Opcode Opc;
1515 switch (Kind) {
1516 default: assert(0 && "Unknown unary op!");
1517 case tok::plusplus: Opc = UnaryOperator::PreInc; break;
1518 case tok::minusminus: Opc = UnaryOperator::PreDec; break;
1519 case tok::amp: Opc = UnaryOperator::AddrOf; break;
1520 case tok::star: Opc = UnaryOperator::Deref; break;
1521 case tok::plus: Opc = UnaryOperator::Plus; break;
1522 case tok::minus: Opc = UnaryOperator::Minus; break;
1523 case tok::tilde: Opc = UnaryOperator::Not; break;
1524 case tok::exclaim: Opc = UnaryOperator::LNot; break;
1525 case tok::kw_sizeof: Opc = UnaryOperator::SizeOf; break;
1526 case tok::kw___alignof: Opc = UnaryOperator::AlignOf; break;
1527 case tok::kw___real: Opc = UnaryOperator::Real; break;
1528 case tok::kw___imag: Opc = UnaryOperator::Imag; break;
1529 case tok::kw___extension__: Opc = UnaryOperator::Extension; break;
1530 }
1531 return Opc;
1532}
1533
1534// Binary Operators. 'Tok' is the token for the operator.
Steve Narofff69936d2007-09-16 03:34:24 +00001535Action::ExprResult Sema::ActOnBinOp(SourceLocation TokLoc, tok::TokenKind Kind,
Reid Spencer5f016e22007-07-11 17:01:13 +00001536 ExprTy *LHS, ExprTy *RHS) {
1537 BinaryOperator::Opcode Opc = ConvertTokenKindToBinaryOpcode(Kind);
1538 Expr *lhs = (Expr *)LHS, *rhs = (Expr*)RHS;
1539
Steve Narofff69936d2007-09-16 03:34:24 +00001540 assert((lhs != 0) && "ActOnBinOp(): missing left expression");
1541 assert((rhs != 0) && "ActOnBinOp(): missing right expression");
Reid Spencer5f016e22007-07-11 17:01:13 +00001542
1543 QualType ResultTy; // Result type of the binary operator.
1544 QualType CompTy; // Computation type for compound assignments (e.g. '+=')
1545
1546 switch (Opc) {
1547 default:
1548 assert(0 && "Unknown binary expr!");
1549 case BinaryOperator::Assign:
1550 ResultTy = CheckAssignmentOperands(lhs, rhs, TokLoc, QualType());
1551 break;
1552 case BinaryOperator::Mul:
1553 case BinaryOperator::Div:
1554 ResultTy = CheckMultiplyDivideOperands(lhs, rhs, TokLoc);
1555 break;
1556 case BinaryOperator::Rem:
1557 ResultTy = CheckRemainderOperands(lhs, rhs, TokLoc);
1558 break;
1559 case BinaryOperator::Add:
1560 ResultTy = CheckAdditionOperands(lhs, rhs, TokLoc);
1561 break;
1562 case BinaryOperator::Sub:
1563 ResultTy = CheckSubtractionOperands(lhs, rhs, TokLoc);
1564 break;
1565 case BinaryOperator::Shl:
1566 case BinaryOperator::Shr:
1567 ResultTy = CheckShiftOperands(lhs, rhs, TokLoc);
1568 break;
1569 case BinaryOperator::LE:
1570 case BinaryOperator::LT:
1571 case BinaryOperator::GE:
1572 case BinaryOperator::GT:
Chris Lattnera5937dd2007-08-26 01:18:55 +00001573 ResultTy = CheckCompareOperands(lhs, rhs, TokLoc, true);
Reid Spencer5f016e22007-07-11 17:01:13 +00001574 break;
1575 case BinaryOperator::EQ:
1576 case BinaryOperator::NE:
Chris Lattnera5937dd2007-08-26 01:18:55 +00001577 ResultTy = CheckCompareOperands(lhs, rhs, TokLoc, false);
Reid Spencer5f016e22007-07-11 17:01:13 +00001578 break;
1579 case BinaryOperator::And:
1580 case BinaryOperator::Xor:
1581 case BinaryOperator::Or:
1582 ResultTy = CheckBitwiseOperands(lhs, rhs, TokLoc);
1583 break;
1584 case BinaryOperator::LAnd:
1585 case BinaryOperator::LOr:
1586 ResultTy = CheckLogicalOperands(lhs, rhs, TokLoc);
1587 break;
1588 case BinaryOperator::MulAssign:
1589 case BinaryOperator::DivAssign:
Steve Naroff9f5fa9b2007-08-24 19:07:16 +00001590 CompTy = CheckMultiplyDivideOperands(lhs, rhs, TokLoc, true);
Reid Spencer5f016e22007-07-11 17:01:13 +00001591 if (!CompTy.isNull())
1592 ResultTy = CheckAssignmentOperands(lhs, rhs, TokLoc, CompTy);
1593 break;
1594 case BinaryOperator::RemAssign:
Steve Naroff9f5fa9b2007-08-24 19:07:16 +00001595 CompTy = CheckRemainderOperands(lhs, rhs, TokLoc, true);
Reid Spencer5f016e22007-07-11 17:01:13 +00001596 if (!CompTy.isNull())
1597 ResultTy = CheckAssignmentOperands(lhs, rhs, TokLoc, CompTy);
1598 break;
1599 case BinaryOperator::AddAssign:
Steve Naroff9f5fa9b2007-08-24 19:07:16 +00001600 CompTy = CheckAdditionOperands(lhs, rhs, TokLoc, true);
Reid Spencer5f016e22007-07-11 17:01:13 +00001601 if (!CompTy.isNull())
1602 ResultTy = CheckAssignmentOperands(lhs, rhs, TokLoc, CompTy);
1603 break;
1604 case BinaryOperator::SubAssign:
Steve Naroff9f5fa9b2007-08-24 19:07:16 +00001605 CompTy = CheckSubtractionOperands(lhs, rhs, TokLoc, true);
Reid Spencer5f016e22007-07-11 17:01:13 +00001606 if (!CompTy.isNull())
1607 ResultTy = CheckAssignmentOperands(lhs, rhs, TokLoc, CompTy);
1608 break;
1609 case BinaryOperator::ShlAssign:
1610 case BinaryOperator::ShrAssign:
Steve Naroff9f5fa9b2007-08-24 19:07:16 +00001611 CompTy = CheckShiftOperands(lhs, rhs, TokLoc, true);
Reid Spencer5f016e22007-07-11 17:01:13 +00001612 if (!CompTy.isNull())
1613 ResultTy = CheckAssignmentOperands(lhs, rhs, TokLoc, CompTy);
1614 break;
1615 case BinaryOperator::AndAssign:
1616 case BinaryOperator::XorAssign:
1617 case BinaryOperator::OrAssign:
Steve Naroff9f5fa9b2007-08-24 19:07:16 +00001618 CompTy = CheckBitwiseOperands(lhs, rhs, TokLoc, true);
Reid Spencer5f016e22007-07-11 17:01:13 +00001619 if (!CompTy.isNull())
1620 ResultTy = CheckAssignmentOperands(lhs, rhs, TokLoc, CompTy);
1621 break;
1622 case BinaryOperator::Comma:
1623 ResultTy = CheckCommaOperands(lhs, rhs, TokLoc);
1624 break;
1625 }
1626 if (ResultTy.isNull())
1627 return true;
1628 if (CompTy.isNull())
Chris Lattner17d1b2a2007-08-28 18:36:55 +00001629 return new BinaryOperator(lhs, rhs, Opc, ResultTy, TokLoc);
Reid Spencer5f016e22007-07-11 17:01:13 +00001630 else
Chris Lattner17d1b2a2007-08-28 18:36:55 +00001631 return new CompoundAssignOperator(lhs, rhs, Opc, ResultTy, CompTy, TokLoc);
Reid Spencer5f016e22007-07-11 17:01:13 +00001632}
1633
1634// Unary Operators. 'Tok' is the token for the operator.
Steve Narofff69936d2007-09-16 03:34:24 +00001635Action::ExprResult Sema::ActOnUnaryOp(SourceLocation OpLoc, tok::TokenKind Op,
Reid Spencer5f016e22007-07-11 17:01:13 +00001636 ExprTy *input) {
1637 Expr *Input = (Expr*)input;
1638 UnaryOperator::Opcode Opc = ConvertTokenKindToUnaryOpcode(Op);
1639 QualType resultType;
1640 switch (Opc) {
1641 default:
1642 assert(0 && "Unimplemented unary expr!");
1643 case UnaryOperator::PreInc:
1644 case UnaryOperator::PreDec:
1645 resultType = CheckIncrementDecrementOperand(Input, OpLoc);
1646 break;
1647 case UnaryOperator::AddrOf:
1648 resultType = CheckAddressOfOperand(Input, OpLoc);
1649 break;
1650 case UnaryOperator::Deref:
1651 resultType = CheckIndirectionOperand(Input, OpLoc);
1652 break;
1653 case UnaryOperator::Plus:
1654 case UnaryOperator::Minus:
Steve Naroffc80b4ee2007-07-16 21:54:35 +00001655 UsualUnaryConversions(Input);
1656 resultType = Input->getType();
Reid Spencer5f016e22007-07-11 17:01:13 +00001657 if (!resultType->isArithmeticType()) // C99 6.5.3.3p1
1658 return Diag(OpLoc, diag::err_typecheck_unary_expr,
1659 resultType.getAsString());
1660 break;
1661 case UnaryOperator::Not: // bitwise complement
Steve Naroffc80b4ee2007-07-16 21:54:35 +00001662 UsualUnaryConversions(Input);
1663 resultType = Input->getType();
Steve Naroff084f9ed2007-08-24 17:20:07 +00001664 // C99 6.5.3.3p1. We allow complex as a GCC extension.
1665 if (!resultType->isIntegerType()) {
1666 if (resultType->isComplexType())
1667 // C99 does not support '~' for complex conjugation.
1668 Diag(OpLoc, diag::ext_integer_complement_complex,
1669 resultType.getAsString());
1670 else
1671 return Diag(OpLoc, diag::err_typecheck_unary_expr,
1672 resultType.getAsString());
1673 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001674 break;
1675 case UnaryOperator::LNot: // logical negation
1676 // Unlike +/-/~, integer promotions aren't done here (C99 6.5.3.3p5).
Steve Naroffc80b4ee2007-07-16 21:54:35 +00001677 DefaultFunctionArrayConversion(Input);
1678 resultType = Input->getType();
Reid Spencer5f016e22007-07-11 17:01:13 +00001679 if (!resultType->isScalarType()) // C99 6.5.3.3p1
1680 return Diag(OpLoc, diag::err_typecheck_unary_expr,
1681 resultType.getAsString());
1682 // LNot always has type int. C99 6.5.3.3p5.
1683 resultType = Context.IntTy;
1684 break;
1685 case UnaryOperator::SizeOf:
1686 resultType = CheckSizeOfAlignOfOperand(Input->getType(), OpLoc, true);
1687 break;
1688 case UnaryOperator::AlignOf:
1689 resultType = CheckSizeOfAlignOfOperand(Input->getType(), OpLoc, false);
1690 break;
Chris Lattnerdbb36972007-08-24 21:16:53 +00001691 case UnaryOperator::Real:
Chris Lattnerdbb36972007-08-24 21:16:53 +00001692 case UnaryOperator::Imag:
Chris Lattner5d794252007-08-24 21:41:10 +00001693 resultType = CheckRealImagOperand(Input, OpLoc);
Chris Lattnerdbb36972007-08-24 21:16:53 +00001694 break;
Reid Spencer5f016e22007-07-11 17:01:13 +00001695 case UnaryOperator::Extension:
Reid Spencer5f016e22007-07-11 17:01:13 +00001696 resultType = Input->getType();
1697 break;
1698 }
1699 if (resultType.isNull())
1700 return true;
1701 return new UnaryOperator(Input, Opc, resultType, OpLoc);
1702}
1703
Steve Naroff1b273c42007-09-16 14:56:35 +00001704/// ActOnAddrLabel - Parse the GNU address of label extension: "&&foo".
1705Sema::ExprResult Sema::ActOnAddrLabel(SourceLocation OpLoc,
Reid Spencer5f016e22007-07-11 17:01:13 +00001706 SourceLocation LabLoc,
1707 IdentifierInfo *LabelII) {
1708 // Look up the record for this label identifier.
1709 LabelStmt *&LabelDecl = LabelMap[LabelII];
1710
1711 // If we haven't seen this label yet, create a forward reference.
1712 if (LabelDecl == 0)
1713 LabelDecl = new LabelStmt(LabLoc, LabelII, 0);
1714
1715 // Create the AST node. The address of a label always has type 'void*'.
Chris Lattner6481a572007-08-03 17:31:20 +00001716 return new AddrLabelExpr(OpLoc, LabLoc, LabelDecl,
1717 Context.getPointerType(Context.VoidTy));
Reid Spencer5f016e22007-07-11 17:01:13 +00001718}
1719
Steve Naroff1b273c42007-09-16 14:56:35 +00001720Sema::ExprResult Sema::ActOnStmtExpr(SourceLocation LPLoc, StmtTy *substmt,
Chris Lattnerab18c4c2007-07-24 16:58:17 +00001721 SourceLocation RPLoc) { // "({..})"
1722 Stmt *SubStmt = static_cast<Stmt*>(substmt);
1723 assert(SubStmt && isa<CompoundStmt>(SubStmt) && "Invalid action invocation!");
1724 CompoundStmt *Compound = cast<CompoundStmt>(SubStmt);
1725
1726 // FIXME: there are a variety of strange constraints to enforce here, for
1727 // example, it is not possible to goto into a stmt expression apparently.
1728 // More semantic analysis is needed.
1729
1730 // FIXME: the last statement in the compount stmt has its value used. We
1731 // should not warn about it being unused.
1732
1733 // If there are sub stmts in the compound stmt, take the type of the last one
1734 // as the type of the stmtexpr.
1735 QualType Ty = Context.VoidTy;
1736
1737 if (!Compound->body_empty())
1738 if (Expr *LastExpr = dyn_cast<Expr>(Compound->body_back()))
1739 Ty = LastExpr->getType();
1740
1741 return new StmtExpr(Compound, Ty, LPLoc, RPLoc);
1742}
Steve Naroffd34e9152007-08-01 22:05:33 +00001743
Steve Naroff1b273c42007-09-16 14:56:35 +00001744Sema::ExprResult Sema::ActOnBuiltinOffsetOf(SourceLocation BuiltinLoc,
Chris Lattner73d0d4f2007-08-30 17:45:32 +00001745 SourceLocation TypeLoc,
1746 TypeTy *argty,
1747 OffsetOfComponent *CompPtr,
1748 unsigned NumComponents,
1749 SourceLocation RPLoc) {
1750 QualType ArgTy = QualType::getFromOpaquePtr(argty);
1751 assert(!ArgTy.isNull() && "Missing type argument!");
1752
1753 // We must have at least one component that refers to the type, and the first
1754 // one is known to be a field designator. Verify that the ArgTy represents
1755 // a struct/union/class.
1756 if (!ArgTy->isRecordType())
1757 return Diag(TypeLoc, diag::err_offsetof_record_type,ArgTy.getAsString());
1758
1759 // Otherwise, create a compound literal expression as the base, and
1760 // iteratively process the offsetof designators.
1761 Expr *Res = new CompoundLiteralExpr(ArgTy, 0);
1762
Chris Lattner9e2b75c2007-08-31 21:49:13 +00001763 // offsetof with non-identifier designators (e.g. "offsetof(x, a.b[c])") are a
1764 // GCC extension, diagnose them.
1765 if (NumComponents != 1)
1766 Diag(BuiltinLoc, diag::ext_offsetof_extended_field_designator,
1767 SourceRange(CompPtr[1].LocStart, CompPtr[NumComponents-1].LocEnd));
1768
Chris Lattner73d0d4f2007-08-30 17:45:32 +00001769 for (unsigned i = 0; i != NumComponents; ++i) {
1770 const OffsetOfComponent &OC = CompPtr[i];
1771 if (OC.isBrackets) {
1772 // Offset of an array sub-field. TODO: Should we allow vector elements?
1773 const ArrayType *AT = Res->getType()->getAsArrayType();
1774 if (!AT) {
1775 delete Res;
1776 return Diag(OC.LocEnd, diag::err_offsetof_array_type,
1777 Res->getType().getAsString());
1778 }
1779
Chris Lattner704fe352007-08-30 17:59:59 +00001780 // FIXME: C++: Verify that operator[] isn't overloaded.
1781
Chris Lattner73d0d4f2007-08-30 17:45:32 +00001782 // C99 6.5.2.1p1
1783 Expr *Idx = static_cast<Expr*>(OC.U.E);
1784 if (!Idx->getType()->isIntegerType())
1785 return Diag(Idx->getLocStart(), diag::err_typecheck_subscript,
1786 Idx->getSourceRange());
1787
1788 Res = new ArraySubscriptExpr(Res, Idx, AT->getElementType(), OC.LocEnd);
1789 continue;
1790 }
1791
1792 const RecordType *RC = Res->getType()->getAsRecordType();
1793 if (!RC) {
1794 delete Res;
1795 return Diag(OC.LocEnd, diag::err_offsetof_record_type,
1796 Res->getType().getAsString());
1797 }
1798
1799 // Get the decl corresponding to this.
1800 RecordDecl *RD = RC->getDecl();
1801 FieldDecl *MemberDecl = RD->getMember(OC.U.IdentInfo);
1802 if (!MemberDecl)
1803 return Diag(BuiltinLoc, diag::err_typecheck_no_member,
1804 OC.U.IdentInfo->getName(),
1805 SourceRange(OC.LocStart, OC.LocEnd));
Chris Lattner704fe352007-08-30 17:59:59 +00001806
1807 // FIXME: C++: Verify that MemberDecl isn't a static field.
1808 // FIXME: Verify that MemberDecl isn't a bitfield.
1809
Chris Lattner73d0d4f2007-08-30 17:45:32 +00001810 Res = new MemberExpr(Res, false, MemberDecl, OC.LocEnd);
1811 }
1812
1813 return new UnaryOperator(Res, UnaryOperator::OffsetOf, Context.getSizeType(),
1814 BuiltinLoc);
1815}
1816
1817
Steve Naroff1b273c42007-09-16 14:56:35 +00001818Sema::ExprResult Sema::ActOnTypesCompatibleExpr(SourceLocation BuiltinLoc,
Steve Naroffd34e9152007-08-01 22:05:33 +00001819 TypeTy *arg1, TypeTy *arg2,
1820 SourceLocation RPLoc) {
1821 QualType argT1 = QualType::getFromOpaquePtr(arg1);
1822 QualType argT2 = QualType::getFromOpaquePtr(arg2);
1823
1824 assert((!argT1.isNull() && !argT2.isNull()) && "Missing type argument(s)");
1825
Chris Lattner73d0d4f2007-08-30 17:45:32 +00001826 return new TypesCompatibleExpr(Context.IntTy, BuiltinLoc, argT1, argT2,RPLoc);
Steve Naroffd34e9152007-08-01 22:05:33 +00001827}
1828
Steve Naroff1b273c42007-09-16 14:56:35 +00001829Sema::ExprResult Sema::ActOnChooseExpr(SourceLocation BuiltinLoc, ExprTy *cond,
Steve Naroffd04fdd52007-08-03 21:21:27 +00001830 ExprTy *expr1, ExprTy *expr2,
1831 SourceLocation RPLoc) {
1832 Expr *CondExpr = static_cast<Expr*>(cond);
1833 Expr *LHSExpr = static_cast<Expr*>(expr1);
1834 Expr *RHSExpr = static_cast<Expr*>(expr2);
1835
1836 assert((CondExpr && LHSExpr && RHSExpr) && "Missing type argument(s)");
1837
1838 // The conditional expression is required to be a constant expression.
1839 llvm::APSInt condEval(32);
1840 SourceLocation ExpLoc;
1841 if (!CondExpr->isIntegerConstantExpr(condEval, Context, &ExpLoc))
1842 return Diag(ExpLoc, diag::err_typecheck_choose_expr_requires_constant,
1843 CondExpr->getSourceRange());
1844
1845 // If the condition is > zero, then the AST type is the same as the LSHExpr.
1846 QualType resType = condEval.getZExtValue() ? LHSExpr->getType() :
1847 RHSExpr->getType();
1848 return new ChooseExpr(BuiltinLoc, CondExpr, LHSExpr, RHSExpr, resType, RPLoc);
1849}
1850
Anders Carlsson55085182007-08-21 17:43:55 +00001851// TODO: Move this to SemaObjC.cpp
Anders Carlssonf9bcf012007-08-22 15:14:15 +00001852Sema::ExprResult Sema::ParseObjCStringLiteral(ExprTy *string) {
Anders Carlsson55085182007-08-21 17:43:55 +00001853 StringLiteral* S = static_cast<StringLiteral *>(string);
1854
1855 if (CheckBuiltinCFStringArgument(S))
1856 return true;
1857
1858 QualType t = Context.getCFConstantStringType();
1859 t = t.getQualifiedType(QualType::Const);
1860 t = Context.getPointerType(t);
1861
1862 return new ObjCStringLiteral(S, t);
1863}
Anders Carlssonf9bcf012007-08-22 15:14:15 +00001864
1865Sema::ExprResult Sema::ParseObjCEncodeExpression(SourceLocation AtLoc,
1866 SourceLocation LParenLoc,
1867 TypeTy *Ty,
1868 SourceLocation RParenLoc) {
1869 QualType EncodedType = QualType::getFromOpaquePtr(Ty);
1870
1871 QualType t = Context.getPointerType(Context.CharTy);
1872 return new ObjCEncodeExpr(t, EncodedType, AtLoc, RParenLoc);
1873}
Steve Naroff708391a2007-09-17 21:01:15 +00001874
Steve Naroff68d331a2007-09-27 14:38:14 +00001875// ActOnClassMessage - used for both unary and keyword messages.
1876// ArgExprs is optional - if it is present, the number of expressions
1877// is obtained from Sel.getNumArgs().
1878Sema::ExprResult Sema::ActOnClassMessage(
Steve Naroff6a8a9a42007-10-02 20:01:56 +00001879 IdentifierInfo *receiverName, Selector Sel,
Steve Naroff68d331a2007-09-27 14:38:14 +00001880 SourceLocation lbrac, SourceLocation rbrac, ExprTy **Args)
Steve Naroff708391a2007-09-17 21:01:15 +00001881{
Steve Naroff6a8a9a42007-10-02 20:01:56 +00001882 assert(receiverName && "missing receiver class name");
Steve Naroff563477d2007-09-18 23:55:05 +00001883
Steve Naroff6a8a9a42007-10-02 20:01:56 +00001884 ObjcInterfaceDecl* ClassDecl = getObjCInterfaceDecl(receiverName);
1885 ObjcMethodDecl *Method = ClassDecl->lookupClassMethod(Sel);
1886 assert(Method && "missing method declaration");
1887 QualType retType = Method->getMethodType();
1888 // Expr *RExpr = global reference to the class symbol...
Steve Naroff68d331a2007-09-27 14:38:14 +00001889 Expr **ArgExprs = reinterpret_cast<Expr **>(Args);
Steve Naroff6a8a9a42007-10-02 20:01:56 +00001890 return new ObjCMessageExpr(receiverName, Sel, retType, lbrac, rbrac, ArgExprs);
Steve Naroff708391a2007-09-17 21:01:15 +00001891}
1892
Steve Naroff68d331a2007-09-27 14:38:14 +00001893// ActOnInstanceMessage - used for both unary and keyword messages.
1894// ArgExprs is optional - if it is present, the number of expressions
1895// is obtained from Sel.getNumArgs().
1896Sema::ExprResult Sema::ActOnInstanceMessage(
Steve Naroffbcfb06a2007-09-28 22:22:11 +00001897 ExprTy *receiver, Selector Sel,
Steve Naroff68d331a2007-09-27 14:38:14 +00001898 SourceLocation lbrac, SourceLocation rbrac, ExprTy **Args)
1899{
Steve Naroff563477d2007-09-18 23:55:05 +00001900 assert(receiver && "missing receiver expression");
1901
1902 Expr *RExpr = static_cast<Expr *>(receiver);
Steve Naroff6a8a9a42007-10-02 20:01:56 +00001903 // FIXME (snaroff): checking in this code from Patrick. Needs to be revisited.
1904 // how do we get the ClassDecl from the receiver expression?
1905 QualType receiverType = RExpr->getType();
1906 while (receiverType->isPointerType()) {
1907 PointerType *pointerType = static_cast<PointerType*>(receiverType.getTypePtr());
1908 receiverType = pointerType->getPointeeType();
1909 }
1910 assert(ObjcInterfaceType::classof(receiverType.getTypePtr()) && "bad receiver type");
1911 ObjcInterfaceDecl* ClassDecl = static_cast<ObjcInterfaceType*>(
1912 receiverType.getTypePtr())->getDecl();
1913 ObjcMethodDecl *Method = ClassDecl->lookupInstanceMethod(Sel);
1914 assert(Method && "missing method declaration");
1915 QualType returnType = Method->getMethodType();
Steve Naroff68d331a2007-09-27 14:38:14 +00001916 Expr **ArgExprs = reinterpret_cast<Expr **>(Args);
Steve Naroff6a8a9a42007-10-02 20:01:56 +00001917 return new ObjCMessageExpr(RExpr, Sel, returnType, lbrac, rbrac, ArgExprs);
Steve Naroff708391a2007-09-17 21:01:15 +00001918}