blob: 31c09be7cb8b67c65c30d5dd7b9bca3ea052c145 [file] [log] [blame]
Chris Lattner5b183d82006-11-10 05:03:26 +00001//===--- SemaExpr.cpp - Semantic Analysis for Expressions -----------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file was developed by Chris Lattner and is distributed under
6// the University of Illinois Open Source License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file implements semantic analysis for expressions.
11//
12//===----------------------------------------------------------------------===//
13
14#include "Sema.h"
Chris Lattnercb6a3822006-11-10 06:20:45 +000015#include "clang/AST/ASTContext.h"
Chris Lattner17ed4872006-11-20 04:58:19 +000016#include "clang/AST/Decl.h"
Steve Naroffc6814ea2007-10-02 20:01:56 +000017#include "clang/AST/DeclObjC.h"
Chris Lattner5b183d82006-11-10 05:03:26 +000018#include "clang/AST/Expr.h"
Steve Naroffd54978b2007-09-18 23:55:05 +000019#include "clang/Parse/DeclSpec.h"
Chris Lattner5b183d82006-11-10 05:03:26 +000020#include "clang/Lex/Preprocessor.h"
Steve Naroff09ef4742007-03-09 23:16:33 +000021#include "clang/Lex/LiteralSupport.h"
Steve Narofff2fb89e2007-03-13 20:29:44 +000022#include "clang/Basic/SourceManager.h"
Chris Lattner5b183d82006-11-10 05:03:26 +000023#include "clang/Basic/Diagnostic.h"
Chris Lattnerac18be92006-11-20 06:49:47 +000024#include "clang/Basic/LangOptions.h"
Chris Lattner5b183d82006-11-10 05:03:26 +000025#include "clang/Basic/TargetInfo.h"
26#include "llvm/ADT/SmallString.h"
Chris Lattnerb87b1b32007-08-10 20:18:51 +000027#include "llvm/ADT/StringExtras.h"
Chris Lattner5b183d82006-11-10 05:03:26 +000028using namespace clang;
29
Steve Naroff83895f72007-09-16 03:34:24 +000030/// ActOnStringLiteral - The specified tokens were lexed as pasted string
Chris Lattner5b183d82006-11-10 05:03:26 +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 Naroff83895f72007-09-16 03:34:24 +000037Sema::ActOnStringLiteral(const Token *StringToks, unsigned NumStringToks) {
Chris Lattner5b183d82006-11-10 05:03:26 +000038 assert(NumStringToks && "Must have at least one string!");
39
Steve Naroff4f88b312007-03-13 22:37:02 +000040 StringLiteralParser Literal(StringToks, NumStringToks, PP, Context.Target);
41 if (Literal.hadError)
42 return ExprResult(true);
Chris Lattner5b183d82006-11-10 05:03:26 +000043
Chris Lattner23b7eb62007-06-15 23:05:46 +000044 llvm::SmallVector<SourceLocation, 4> StringTokLocs;
Chris Lattner5b183d82006-11-10 05:03:26 +000045 for (unsigned i = 0; i != NumStringToks; ++i)
46 StringTokLocs.push_back(StringToks[i].getLocation());
Steve Narofff1e53692007-03-23 22:27:02 +000047
48 // FIXME: handle wchar_t
Anders Carlssoncbfc4b82007-10-15 02:50:23 +000049 QualType t;
50
51 if (Literal.Pascal)
52 t = Context.getPointerType(Context.UnsignedCharTy);
53 else
54 t = Context.getPointerType(Context.CharTy);
55
56 if (Literal.Pascal && Literal.GetStringLength() > 256)
57 return Diag(StringToks[0].getLocation(), diag::err_pascal_string_too_long,
58 SourceRange(StringToks[0].getLocation(),
59 StringToks[NumStringToks-1].getLocation()));
Steve Narofff1e53692007-03-23 22:27:02 +000060
Chris Lattner5b183d82006-11-10 05:03:26 +000061 // Pass &StringTokLocs[0], StringTokLocs.size() to factory!
Steve Naroff4f88b312007-03-13 22:37:02 +000062 return new StringLiteral(Literal.GetString(), Literal.GetStringLength(),
Anders Carlssoncbfc4b82007-10-15 02:50:23 +000063 Literal.AnyWide, t,
64 StringToks[0].getLocation(),
Steve Naroff53f07dc2007-05-17 21:49:33 +000065 StringToks[NumStringToks-1].getLocation());
Chris Lattner5b183d82006-11-10 05:03:26 +000066}
67
Chris Lattnere168f762006-11-10 05:29:30 +000068
Steve Naroff30d242c2007-09-15 18:49:24 +000069/// ActOnIdentifierExpr - The parser read an identifier in expression context,
Chris Lattnerac18be92006-11-20 06:49:47 +000070/// validate it per-C99 6.5.1. HasTrailingLParen indicates whether this
71/// identifier is used in an function call context.
Steve Naroff30d242c2007-09-15 18:49:24 +000072Sema::ExprResult Sema::ActOnIdentifierExpr(Scope *S, SourceLocation Loc,
Chris Lattnerac18be92006-11-20 06:49:47 +000073 IdentifierInfo &II,
74 bool HasTrailingLParen) {
Chris Lattner17ed4872006-11-20 04:58:19 +000075 // Could be enum-constant or decl.
Steve Naroff2f742082007-09-16 16:16:00 +000076 ScopedDecl *D = LookupScopedDecl(&II, Decl::IDNS_Ordinary, Loc, S);
Chris Lattner17ed4872006-11-20 04:58:19 +000077 if (D == 0) {
Bill Wendling4073ed52007-02-13 01:51:42 +000078 // Otherwise, this could be an implicitly declared function reference (legal
Chris Lattner9561a0b2007-01-28 08:20:04 +000079 // in C90, extension in C99).
Chris Lattnerac18be92006-11-20 06:49:47 +000080 if (HasTrailingLParen &&
81 // Not in C++.
Steve Narofff1e53692007-03-23 22:27:02 +000082 !getLangOptions().CPlusPlus)
Chris Lattnerac18be92006-11-20 06:49:47 +000083 D = ImplicitlyDefineFunction(Loc, II, S);
Steve Naroff92e30f82007-04-02 22:35:25 +000084 else {
Chris Lattnerac18be92006-11-20 06:49:47 +000085 // If this name wasn't predeclared and if this is not a function call,
86 // diagnose the problem.
Steve Narofff1e53692007-03-23 22:27:02 +000087 return Diag(Loc, diag::err_undeclared_var_use, II.getName());
Steve Naroff92e30f82007-04-02 22:35:25 +000088 }
Chris Lattner17ed4872006-11-20 04:58:19 +000089 }
Steve Naroff7e6f7c22007-08-28 03:03:08 +000090 if (ValueDecl *VD = dyn_cast<ValueDecl>(D)) {
Steve Naroffcf871f52007-08-28 18:45:29 +000091 // Only create DeclRefExpr's for valid Decl's.
Steve Narofff93b6722007-08-28 20:14:24 +000092 if (VD->isInvalidDecl())
Steve Naroff7e6f7c22007-08-28 03:03:08 +000093 return true;
Steve Naroff509fe022007-05-17 01:16:00 +000094 return new DeclRefExpr(VD, VD->getType(), Loc);
Steve Naroff7e6f7c22007-08-28 03:03:08 +000095 }
Steve Naroff46ba1eb2007-04-03 23:13:13 +000096 if (isa<TypedefDecl>(D))
Steve Narofff1e53692007-03-23 22:27:02 +000097 return Diag(Loc, diag::err_unexpected_typedef, II.getName());
98
99 assert(0 && "Invalid decl");
Chris Lattnerbb0ab462007-07-21 04:57:45 +0000100 abort();
Chris Lattner17ed4872006-11-20 04:58:19 +0000101}
Chris Lattnere168f762006-11-10 05:29:30 +0000102
Steve Naroff83895f72007-09-16 03:34:24 +0000103Sema::ExprResult Sema::ActOnPreDefinedExpr(SourceLocation Loc,
Anders Carlsson625bfc82007-07-21 05:21:51 +0000104 tok::TokenKind Kind) {
105 PreDefinedExpr::IdentType IT;
106
Chris Lattnere168f762006-11-10 05:29:30 +0000107 switch (Kind) {
108 default:
109 assert(0 && "Unknown simple primary expr!");
Chris Lattnere168f762006-11-10 05:29:30 +0000110 case tok::kw___func__: // primary-expression: __func__ [C99 6.4.2.2]
Anders Carlsson625bfc82007-07-21 05:21:51 +0000111 IT = PreDefinedExpr::Func;
112 break;
Chris Lattnere168f762006-11-10 05:29:30 +0000113 case tok::kw___FUNCTION__: // primary-expression: __FUNCTION__ [GNU]
Anders Carlsson625bfc82007-07-21 05:21:51 +0000114 IT = PreDefinedExpr::Function;
115 break;
Chris Lattnere168f762006-11-10 05:29:30 +0000116 case tok::kw___PRETTY_FUNCTION__: // primary-expression: __P..Y_F..N__ [GNU]
Anders Carlsson625bfc82007-07-21 05:21:51 +0000117 IT = PreDefinedExpr::PrettyFunction;
118 break;
Chris Lattnere168f762006-11-10 05:29:30 +0000119 }
Anders Carlsson625bfc82007-07-21 05:21:51 +0000120
121 // Pre-defined identifiers are always of type char *.
122 return new PreDefinedExpr(Loc, Context.getPointerType(Context.CharTy), IT);
Chris Lattnere168f762006-11-10 05:29:30 +0000123}
124
Steve Naroff83895f72007-09-16 03:34:24 +0000125Sema::ExprResult Sema::ActOnCharacterConstant(const Token &Tok) {
Chris Lattner23b7eb62007-06-15 23:05:46 +0000126 llvm::SmallString<16> CharBuffer;
Steve Naroffae4143e2007-04-26 20:39:23 +0000127 CharBuffer.resize(Tok.getLength());
128 const char *ThisTokBegin = &CharBuffer[0];
129 unsigned ActualLength = PP.getSpelling(Tok, ThisTokBegin);
130
131 CharLiteralParser Literal(ThisTokBegin, ThisTokBegin+ActualLength,
132 Tok.getLocation(), PP);
133 if (Literal.hadError())
134 return ExprResult(true);
Steve Naroff509fe022007-05-17 01:16:00 +0000135 return new CharacterLiteral(Literal.getValue(), Context.IntTy,
136 Tok.getLocation());
Steve Naroffae4143e2007-04-26 20:39:23 +0000137}
138
Steve Naroff83895f72007-09-16 03:34:24 +0000139Action::ExprResult Sema::ActOnNumericConstant(const Token &Tok) {
Steve Narofff2fb89e2007-03-13 20:29:44 +0000140 // fast path for a single digit (which is quite common). A single digit
141 // cannot have a trigraph, escaped newline, radix prefix, or type suffix.
142 if (Tok.getLength() == 1) {
143 const char *t = PP.getSourceManager().getCharacterData(Tok.getLocation());
Chris Lattner67ca9252007-05-21 01:08:44 +0000144
Chris Lattner9cf21c52007-09-04 02:45:27 +0000145 unsigned IntSize = static_cast<unsigned>(
146 Context.getTypeSize(Context.IntTy, Tok.getLocation()));
Chris Lattner23b7eb62007-06-15 23:05:46 +0000147 return ExprResult(new IntegerLiteral(llvm::APInt(IntSize, *t-'0'),
148 Context.IntTy,
Steve Naroff509fe022007-05-17 01:16:00 +0000149 Tok.getLocation()));
Steve Narofff2fb89e2007-03-13 20:29:44 +0000150 }
Chris Lattner23b7eb62007-06-15 23:05:46 +0000151 llvm::SmallString<512> IntegerBuffer;
Steve Naroff8160ea22007-03-06 01:09:46 +0000152 IntegerBuffer.resize(Tok.getLength());
153 const char *ThisTokBegin = &IntegerBuffer[0];
154
Chris Lattner67ca9252007-05-21 01:08:44 +0000155 // Get the spelling of the token, which eliminates trigraphs, etc.
Steve Naroff8160ea22007-03-06 01:09:46 +0000156 unsigned ActualLength = PP.getSpelling(Tok, ThisTokBegin);
Steve Naroff09ef4742007-03-09 23:16:33 +0000157 NumericLiteralParser Literal(ThisTokBegin, ThisTokBegin+ActualLength,
Steve Naroff451d8f162007-03-12 23:22:38 +0000158 Tok.getLocation(), PP);
Steve Narofff2fb89e2007-03-13 20:29:44 +0000159 if (Literal.hadError)
160 return ExprResult(true);
Chris Lattner67ca9252007-05-21 01:08:44 +0000161
Chris Lattner1c20a172007-08-26 03:42:43 +0000162 Expr *Res;
163
164 if (Literal.isFloatingLiteral()) {
Chris Lattnerec0a6d92007-09-22 18:29:59 +0000165 QualType Ty;
166 const llvm::fltSemantics *Format;
167 uint64_t Size; unsigned Align;
168
169 if (Literal.isFloat) {
170 Ty = Context.FloatTy;
171 Context.Target.getFloatInfo(Size, Align, Format, Tok.getLocation());
172 } else if (Literal.isLong) {
173 Ty = Context.LongDoubleTy;
174 Context.Target.getLongDoubleInfo(Size, Align, Format, Tok.getLocation());
175 } else {
176 Ty = Context.DoubleTy;
177 Context.Target.getDoubleInfo(Size, Align, Format, Tok.getLocation());
178 }
179
180 Res = new FloatingLiteral(Literal.GetFloatValue(*Format), Ty,
181 Tok.getLocation());
Chris Lattner1c20a172007-08-26 03:42:43 +0000182 } else if (!Literal.isIntegerLiteral()) {
183 return ExprResult(true);
184 } else {
Steve Naroffe5aa9be2007-04-05 22:36:20 +0000185 QualType t;
Chris Lattner67ca9252007-05-21 01:08:44 +0000186
Neil Boothac582c52007-08-29 22:00:19 +0000187 // long long is a C99 feature.
188 if (!getLangOptions().C99 && !getLangOptions().CPlusPlus0x &&
Neil Booth4a1ee052007-08-29 22:13:52 +0000189 Literal.isLongLong)
Neil Boothac582c52007-08-29 22:00:19 +0000190 Diag(Tok.getLocation(), diag::ext_longlong);
191
Chris Lattner67ca9252007-05-21 01:08:44 +0000192 // Get the value in the widest-possible width.
Chris Lattner23b7eb62007-06-15 23:05:46 +0000193 llvm::APInt ResultVal(Context.Target.getIntMaxTWidth(Tok.getLocation()), 0);
Chris Lattner67ca9252007-05-21 01:08:44 +0000194
195 if (Literal.GetIntegerValue(ResultVal)) {
196 // If this value didn't fit into uintmax_t, warn and force to ull.
197 Diag(Tok.getLocation(), diag::warn_integer_too_large);
198 t = Context.UnsignedLongLongTy;
Chris Lattner4481b422007-07-14 01:29:45 +0000199 assert(Context.getTypeSize(t, Tok.getLocation()) ==
Chris Lattner67ca9252007-05-21 01:08:44 +0000200 ResultVal.getBitWidth() && "long long is not intmax_t?");
Steve Naroff09ef4742007-03-09 23:16:33 +0000201 } else {
Chris Lattner67ca9252007-05-21 01:08:44 +0000202 // If this value fits into a ULL, try to figure out what else it fits into
203 // according to the rules of C99 6.4.4.1p5.
204
205 // Octal, Hexadecimal, and integers with a U suffix are allowed to
206 // be an unsigned int.
207 bool AllowUnsigned = Literal.isUnsigned || Literal.getRadix() != 10;
208
209 // Check from smallest to largest, picking the smallest type we can.
Chris Lattner7b939cf2007-08-23 21:58:08 +0000210 if (!Literal.isLong && !Literal.isLongLong) {
211 // Are int/unsigned possibilities?
Chris Lattner9cf21c52007-09-04 02:45:27 +0000212 unsigned IntSize = static_cast<unsigned>(
213 Context.getTypeSize(Context.IntTy,Tok.getLocation()));
Chris Lattner67ca9252007-05-21 01:08:44 +0000214 // Does it fit in a unsigned int?
215 if (ResultVal.isIntN(IntSize)) {
216 // Does it fit in a signed int?
217 if (!Literal.isUnsigned && ResultVal[IntSize-1] == 0)
218 t = Context.IntTy;
219 else if (AllowUnsigned)
220 t = Context.UnsignedIntTy;
221 }
222
223 if (!t.isNull())
224 ResultVal.trunc(IntSize);
225 }
226
227 // Are long/unsigned long possibilities?
228 if (t.isNull() && !Literal.isLongLong) {
Chris Lattner9cf21c52007-09-04 02:45:27 +0000229 unsigned LongSize = static_cast<unsigned>(
230 Context.getTypeSize(Context.LongTy, Tok.getLocation()));
Chris Lattner67ca9252007-05-21 01:08:44 +0000231
232 // Does it fit in a unsigned long?
233 if (ResultVal.isIntN(LongSize)) {
234 // Does it fit in a signed long?
235 if (!Literal.isUnsigned && ResultVal[LongSize-1] == 0)
236 t = Context.LongTy;
237 else if (AllowUnsigned)
238 t = Context.UnsignedLongTy;
239 }
240 if (!t.isNull())
241 ResultVal.trunc(LongSize);
242 }
243
244 // Finally, check long long if needed.
245 if (t.isNull()) {
Chris Lattner9cf21c52007-09-04 02:45:27 +0000246 unsigned LongLongSize = static_cast<unsigned>(
247 Context.getTypeSize(Context.LongLongTy, Tok.getLocation()));
Chris Lattner67ca9252007-05-21 01:08:44 +0000248
249 // Does it fit in a unsigned long long?
250 if (ResultVal.isIntN(LongLongSize)) {
251 // Does it fit in a signed long long?
252 if (!Literal.isUnsigned && ResultVal[LongLongSize-1] == 0)
253 t = Context.LongLongTy;
254 else if (AllowUnsigned)
255 t = Context.UnsignedLongLongTy;
256 }
257 }
258
259 // If we still couldn't decide a type, we probably have something that
260 // does not fit in a signed long long, but has no U suffix.
261 if (t.isNull()) {
262 Diag(Tok.getLocation(), diag::warn_integer_too_large_for_signed);
263 t = Context.UnsignedLongLongTy;
264 }
Steve Naroff09ef4742007-03-09 23:16:33 +0000265 }
Chris Lattner67ca9252007-05-21 01:08:44 +0000266
Chris Lattner1c20a172007-08-26 03:42:43 +0000267 Res = new IntegerLiteral(ResultVal, t, Tok.getLocation());
Steve Naroff09ef4742007-03-09 23:16:33 +0000268 }
Chris Lattner1c20a172007-08-26 03:42:43 +0000269
270 // If this is an imaginary literal, create the ImaginaryLiteral wrapper.
271 if (Literal.isImaginary)
272 Res = new ImaginaryLiteral(Res, Context.getComplexType(Res->getType()));
273
274 return Res;
Chris Lattnere168f762006-11-10 05:29:30 +0000275}
276
Steve Naroff83895f72007-09-16 03:34:24 +0000277Action::ExprResult Sema::ActOnParenExpr(SourceLocation L, SourceLocation R,
Chris Lattnere168f762006-11-10 05:29:30 +0000278 ExprTy *Val) {
Steve Naroffae4143e2007-04-26 20:39:23 +0000279 Expr *e = (Expr *)Val;
Steve Naroff83895f72007-09-16 03:34:24 +0000280 assert((e != 0) && "ActOnParenExpr() missing expr");
Steve Naroff53f07dc2007-05-17 21:49:33 +0000281 return new ParenExpr(L, R, e);
Chris Lattnere168f762006-11-10 05:29:30 +0000282}
283
Steve Naroff71b59a92007-06-04 22:22:31 +0000284/// The UsualUnaryConversions() function is *not* called by this routine.
Steve Naroffa78fe7e2007-05-16 19:47:19 +0000285/// See C99 6.3.2.1p[2-4] for more details.
Steve Naroff043d45d2007-05-15 02:32:35 +0000286QualType Sema::CheckSizeOfAlignOfOperand(QualType exprType,
287 SourceLocation OpLoc, bool isSizeof) {
288 // C99 6.5.3.4p1:
289 if (isa<FunctionType>(exprType) && isSizeof)
290 // alignof(function) is allowed.
291 Diag(OpLoc, diag::ext_sizeof_function_type);
292 else if (exprType->isVoidType())
293 Diag(OpLoc, diag::ext_sizeof_void_type, isSizeof ? "sizeof" : "__alignof");
294 else if (exprType->isIncompleteType()) {
Steve Naroff043d45d2007-05-15 02:32:35 +0000295 Diag(OpLoc, isSizeof ? diag::err_sizeof_incomplete_type :
Chris Lattner3dc3d772007-05-16 18:07:12 +0000296 diag::err_alignof_incomplete_type,
297 exprType.getAsString());
Steve Naroff043d45d2007-05-15 02:32:35 +0000298 return QualType(); // error
299 }
300 // C99 6.5.3.4p4: the type (an unsigned integer type) is size_t.
301 return Context.getSizeType();
302}
303
Chris Lattnere168f762006-11-10 05:29:30 +0000304Action::ExprResult Sema::
Steve Naroff83895f72007-09-16 03:34:24 +0000305ActOnSizeOfAlignOfTypeExpr(SourceLocation OpLoc, bool isSizeof,
Steve Naroff509fe022007-05-17 01:16:00 +0000306 SourceLocation LPLoc, TypeTy *Ty,
307 SourceLocation RPLoc) {
Chris Lattner0d8b1a12006-11-20 04:34:45 +0000308 // If error parsing type, ignore.
309 if (Ty == 0) return true;
Chris Lattner6531c102007-01-23 22:29:49 +0000310
311 // Verify that this is a valid expression.
Steve Naroffe5aa9be2007-04-05 22:36:20 +0000312 QualType ArgTy = QualType::getFromOpaquePtr(Ty);
Chris Lattner6531c102007-01-23 22:29:49 +0000313
Steve Naroff043d45d2007-05-15 02:32:35 +0000314 QualType resultType = CheckSizeOfAlignOfOperand(ArgTy, OpLoc, isSizeof);
315
316 if (resultType.isNull())
317 return true;
Steve Naroff509fe022007-05-17 01:16:00 +0000318 return new SizeOfAlignOfTypeExpr(isSizeof, ArgTy, resultType, OpLoc, RPLoc);
Chris Lattnere168f762006-11-10 05:29:30 +0000319}
320
Chris Lattner74ed76b2007-08-24 21:41:10 +0000321QualType Sema::CheckRealImagOperand(Expr *&V, SourceLocation Loc) {
Chris Lattner30b5dd02007-08-24 21:16:53 +0000322 DefaultFunctionArrayConversion(V);
323
Chris Lattnere267f5d2007-08-26 05:39:26 +0000324 // These operators return the element type of a complex type.
Chris Lattner30b5dd02007-08-24 21:16:53 +0000325 if (const ComplexType *CT = V->getType()->getAsComplexType())
326 return CT->getElementType();
Chris Lattnere267f5d2007-08-26 05:39:26 +0000327
328 // Otherwise they pass through real integer and floating point types here.
329 if (V->getType()->isArithmeticType())
330 return V->getType();
331
332 // Reject anything else.
333 Diag(Loc, diag::err_realimag_invalid_type, V->getType().getAsString());
334 return QualType();
Chris Lattner30b5dd02007-08-24 21:16:53 +0000335}
336
337
Chris Lattnere168f762006-11-10 05:29:30 +0000338
Steve Naroff83895f72007-09-16 03:34:24 +0000339Action::ExprResult Sema::ActOnPostfixUnaryOp(SourceLocation OpLoc,
Chris Lattnere168f762006-11-10 05:29:30 +0000340 tok::TokenKind Kind,
341 ExprTy *Input) {
342 UnaryOperator::Opcode Opc;
343 switch (Kind) {
344 default: assert(0 && "Unknown unary op!");
345 case tok::plusplus: Opc = UnaryOperator::PostInc; break;
346 case tok::minusminus: Opc = UnaryOperator::PostDec; break;
347 }
Steve Naroff71ce2e02007-05-18 22:53:50 +0000348 QualType result = CheckIncrementDecrementOperand((Expr *)Input, OpLoc);
Steve Naroff35d85152007-05-07 00:24:15 +0000349 if (result.isNull())
350 return true;
Steve Naroff509fe022007-05-17 01:16:00 +0000351 return new UnaryOperator((Expr *)Input, Opc, result, OpLoc);
Chris Lattnere168f762006-11-10 05:29:30 +0000352}
353
354Action::ExprResult Sema::
Steve Naroff83895f72007-09-16 03:34:24 +0000355ActOnArraySubscriptExpr(ExprTy *Base, SourceLocation LLoc,
Chris Lattnere168f762006-11-10 05:29:30 +0000356 ExprTy *Idx, SourceLocation RLoc) {
Chris Lattner5981db42007-07-15 23:59:53 +0000357 Expr *LHSExp = static_cast<Expr*>(Base), *RHSExp = static_cast<Expr*>(Idx);
Chris Lattner36d572b2007-07-16 00:14:47 +0000358
359 // Perform default conversions.
360 DefaultFunctionArrayConversion(LHSExp);
361 DefaultFunctionArrayConversion(RHSExp);
Chris Lattner5981db42007-07-15 23:59:53 +0000362
Chris Lattner36d572b2007-07-16 00:14:47 +0000363 QualType LHSTy = LHSExp->getType(), RHSTy = RHSExp->getType();
Steve Narofff1e53692007-03-23 22:27:02 +0000364
Steve Naroffc1aadb12007-03-28 21:49:40 +0000365 // C99 6.5.2.1p2: the expression e1[e2] is by definition precisely equivalent
Chris Lattnerf17bd422007-08-30 17:45:32 +0000366 // to the expression *((e1)+(e2)). This means the array "Base" may actually be
Steve Narofff1e53692007-03-23 22:27:02 +0000367 // in the subscript position. As a result, we need to derive the array base
368 // and index from the expression types.
Chris Lattner36d572b2007-07-16 00:14:47 +0000369 Expr *BaseExpr, *IndexExpr;
370 QualType ResultType;
Chris Lattnerc996b172007-07-31 16:53:04 +0000371 if (const PointerType *PTy = LHSTy->getAsPointerType()) {
Chris Lattner36d572b2007-07-16 00:14:47 +0000372 BaseExpr = LHSExp;
373 IndexExpr = RHSExp;
374 // FIXME: need to deal with const...
375 ResultType = PTy->getPointeeType();
Chris Lattnerc996b172007-07-31 16:53:04 +0000376 } else if (const PointerType *PTy = RHSTy->getAsPointerType()) {
Chris Lattneraee0cfd2007-07-16 00:23:25 +0000377 // Handle the uncommon case of "123[Ptr]".
Chris Lattner36d572b2007-07-16 00:14:47 +0000378 BaseExpr = RHSExp;
379 IndexExpr = LHSExp;
380 // FIXME: need to deal with const...
381 ResultType = PTy->getPointeeType();
Chris Lattner41977962007-07-31 19:29:30 +0000382 } else if (const VectorType *VTy = LHSTy->getAsVectorType()) {
383 BaseExpr = LHSExp; // vectors: V[123]
Chris Lattner36d572b2007-07-16 00:14:47 +0000384 IndexExpr = RHSExp;
Steve Naroff01047312007-08-03 22:40:33 +0000385
386 // Component access limited to variables (reject vec4.rg[1]).
387 if (!isa<DeclRefExpr>(BaseExpr))
388 return Diag(LLoc, diag::err_ocuvector_component_access,
389 SourceRange(LLoc, RLoc));
Chris Lattner36d572b2007-07-16 00:14:47 +0000390 // FIXME: need to deal with const...
391 ResultType = VTy->getElementType();
Steve Naroffb3096442007-06-09 03:47:53 +0000392 } else {
Chris Lattner5981db42007-07-15 23:59:53 +0000393 return Diag(LHSExp->getLocStart(), diag::err_typecheck_subscript_value,
394 RHSExp->getSourceRange());
Steve Naroffb3096442007-06-09 03:47:53 +0000395 }
Steve Naroffc1aadb12007-03-28 21:49:40 +0000396 // C99 6.5.2.1p1
Chris Lattner36d572b2007-07-16 00:14:47 +0000397 if (!IndexExpr->getType()->isIntegerType())
398 return Diag(IndexExpr->getLocStart(), diag::err_typecheck_subscript,
399 IndexExpr->getSourceRange());
Steve Naroffb29cdd52007-07-10 18:23:31 +0000400
Chris Lattner36d572b2007-07-16 00:14:47 +0000401 // C99 6.5.2.1p1: "shall have type "pointer to *object* type". In practice,
402 // the following check catches trying to index a pointer to a function (e.g.
403 // void (*)(int)). Functions are not objects in C99.
404 if (!ResultType->isObjectType())
405 return Diag(BaseExpr->getLocStart(),
406 diag::err_typecheck_subscript_not_object,
407 BaseExpr->getType().getAsString(), BaseExpr->getSourceRange());
408
409 return new ArraySubscriptExpr(LHSExp, RHSExp, ResultType, RLoc);
Chris Lattnere168f762006-11-10 05:29:30 +0000410}
411
Steve Narofff8fd09e2007-07-27 22:15:19 +0000412QualType Sema::
413CheckOCUVectorComponent(QualType baseType, SourceLocation OpLoc,
414 IdentifierInfo &CompName, SourceLocation CompLoc) {
Chris Lattner41977962007-07-31 19:29:30 +0000415 const OCUVectorType *vecType = baseType->getAsOCUVectorType();
Steve Narofff8fd09e2007-07-27 22:15:19 +0000416
417 // The vector accessor can't exceed the number of elements.
418 const char *compStr = CompName.getName();
419 if (strlen(compStr) > vecType->getNumElements()) {
420 Diag(OpLoc, diag::err_ocuvector_component_exceeds_length,
421 baseType.getAsString(), SourceRange(CompLoc));
422 return QualType();
423 }
424 // The component names must come from the same set.
Chris Lattner7e152db2007-08-02 22:33:49 +0000425 if (vecType->getPointAccessorIdx(*compStr) != -1) {
426 do
427 compStr++;
428 while (*compStr && vecType->getPointAccessorIdx(*compStr) != -1);
429 } else if (vecType->getColorAccessorIdx(*compStr) != -1) {
430 do
431 compStr++;
432 while (*compStr && vecType->getColorAccessorIdx(*compStr) != -1);
433 } else if (vecType->getTextureAccessorIdx(*compStr) != -1) {
434 do
435 compStr++;
436 while (*compStr && vecType->getTextureAccessorIdx(*compStr) != -1);
437 }
Steve Narofff8fd09e2007-07-27 22:15:19 +0000438
439 if (*compStr) {
440 // We didn't get to the end of the string. This means the component names
441 // didn't come from the same set *or* we encountered an illegal name.
442 Diag(OpLoc, diag::err_ocuvector_component_name_illegal,
443 std::string(compStr,compStr+1), SourceRange(CompLoc));
444 return QualType();
445 }
446 // Each component accessor can't exceed the vector type.
447 compStr = CompName.getName();
448 while (*compStr) {
449 if (vecType->isAccessorWithinNumElements(*compStr))
450 compStr++;
451 else
452 break;
453 }
454 if (*compStr) {
455 // We didn't get to the end of the string. This means a component accessor
456 // exceeds the number of elements in the vector.
457 Diag(OpLoc, diag::err_ocuvector_component_exceeds_length,
458 baseType.getAsString(), SourceRange(CompLoc));
459 return QualType();
460 }
461 // The component accessor looks fine - now we need to compute the actual type.
462 // The vector type is implied by the component accessor. For example,
463 // vec4.b is a float, vec4.xy is a vec2, vec4.rgb is a vec3, etc.
464 unsigned CompSize = strlen(CompName.getName());
465 if (CompSize == 1)
466 return vecType->getElementType();
Steve Naroffddf5a1d2007-07-29 16:33:31 +0000467
468 QualType VT = Context.getOCUVectorType(vecType->getElementType(), CompSize);
469 // Now look up the TypeDefDecl from the vector type. Without this,
470 // diagostics look bad. We want OCU vector types to appear built-in.
471 for (unsigned i = 0, e = OCUVectorDecls.size(); i != e; ++i) {
472 if (OCUVectorDecls[i]->getUnderlyingType() == VT)
473 return Context.getTypedefType(OCUVectorDecls[i]);
474 }
475 return VT; // should never get here (a typedef type should always be found).
Steve Narofff8fd09e2007-07-27 22:15:19 +0000476}
477
Chris Lattnere168f762006-11-10 05:29:30 +0000478Action::ExprResult Sema::
Steve Naroff83895f72007-09-16 03:34:24 +0000479ActOnMemberReferenceExpr(ExprTy *Base, SourceLocation OpLoc,
Chris Lattnere168f762006-11-10 05:29:30 +0000480 tok::TokenKind OpKind, SourceLocation MemberLoc,
481 IdentifierInfo &Member) {
Steve Naroff185616f2007-07-26 03:11:44 +0000482 Expr *BaseExpr = static_cast<Expr *>(Base);
483 assert(BaseExpr && "no record expression");
Steve Naroffca8f7122007-04-01 01:41:35 +0000484
Steve Naroff185616f2007-07-26 03:11:44 +0000485 QualType BaseType = BaseExpr->getType();
486 assert(!BaseType.isNull() && "no type for member expression");
Steve Naroffca8f7122007-04-01 01:41:35 +0000487
Steve Narofff1e53692007-03-23 22:27:02 +0000488 if (OpKind == tok::arrow) {
Chris Lattnerc996b172007-07-31 16:53:04 +0000489 if (const PointerType *PT = BaseType->getAsPointerType())
Steve Naroff185616f2007-07-26 03:11:44 +0000490 BaseType = PT->getPointeeType();
491 else
492 return Diag(OpLoc, diag::err_typecheck_member_reference_arrow,
493 SourceRange(MemberLoc));
Steve Narofff1e53692007-03-23 22:27:02 +0000494 }
Steve Narofff8fd09e2007-07-27 22:15:19 +0000495 // The base type is either a record or an OCUVectorType.
Chris Lattner41977962007-07-31 19:29:30 +0000496 if (const RecordType *RTy = BaseType->getAsRecordType()) {
Steve Naroff185616f2007-07-26 03:11:44 +0000497 RecordDecl *RDecl = RTy->getDecl();
498 if (RTy->isIncompleteType())
499 return Diag(OpLoc, diag::err_typecheck_incomplete_tag, RDecl->getName(),
500 BaseExpr->getSourceRange());
501 // The record definition is complete, now make sure the member is valid.
Steve Narofff8fd09e2007-07-27 22:15:19 +0000502 FieldDecl *MemberDecl = RDecl->getMember(&Member);
503 if (!MemberDecl)
Steve Naroff185616f2007-07-26 03:11:44 +0000504 return Diag(OpLoc, diag::err_typecheck_no_member, Member.getName(),
505 SourceRange(MemberLoc));
Steve Narofff8fd09e2007-07-27 22:15:19 +0000506 return new MemberExpr(BaseExpr, OpKind==tok::arrow, MemberDecl, MemberLoc);
507 } else if (BaseType->isOCUVectorType() && OpKind == tok::period) {
Steve Naroff01047312007-08-03 22:40:33 +0000508 // Component access limited to variables (reject vec4.rg.g).
509 if (!isa<DeclRefExpr>(BaseExpr))
510 return Diag(OpLoc, diag::err_ocuvector_component_access,
511 SourceRange(MemberLoc));
Steve Narofff8fd09e2007-07-27 22:15:19 +0000512 QualType ret = CheckOCUVectorComponent(BaseType, OpLoc, Member, MemberLoc);
513 if (ret.isNull())
514 return true;
Chris Lattnerd268a7a2007-08-03 17:31:20 +0000515 return new OCUVectorElementExpr(ret, BaseExpr, Member, MemberLoc);
Steve Naroff185616f2007-07-26 03:11:44 +0000516 } else
517 return Diag(OpLoc, diag::err_typecheck_member_reference_structUnion,
518 SourceRange(MemberLoc));
Chris Lattnere168f762006-11-10 05:29:30 +0000519}
520
Steve Naroff83895f72007-09-16 03:34:24 +0000521/// ActOnCallExpr - Handle a call to Fn with the specified array of arguments.
Chris Lattnere168f762006-11-10 05:29:30 +0000522/// This provides the location of the left/right parens and a list of comma
523/// locations.
524Action::ExprResult Sema::
Steve Naroff83895f72007-09-16 03:34:24 +0000525ActOnCallExpr(ExprTy *fn, SourceLocation LParenLoc,
Chris Lattner38dbdb22007-07-21 03:03:59 +0000526 ExprTy **args, unsigned NumArgsInCall,
Chris Lattnere168f762006-11-10 05:29:30 +0000527 SourceLocation *CommaLocs, SourceLocation RParenLoc) {
Chris Lattner38dbdb22007-07-21 03:03:59 +0000528 Expr *Fn = static_cast<Expr *>(fn);
529 Expr **Args = reinterpret_cast<Expr**>(args);
530 assert(Fn && "no function call expression");
Steve Naroff8563f652007-05-28 19:25:56 +0000531
Chris Lattner38dbdb22007-07-21 03:03:59 +0000532 UsualUnaryConversions(Fn);
533 QualType funcType = Fn->getType();
Steve Naroffae4143e2007-04-26 20:39:23 +0000534
Chris Lattner3d01e4e2007-06-06 05:14:05 +0000535 // C99 6.5.2.2p1 - "The expression that denotes the called function shall have
536 // type pointer to function".
Chris Lattnere5a6cbd2007-07-31 21:27:01 +0000537 const PointerType *PT = funcType->getAsPointerType();
Chris Lattner3d01e4e2007-06-06 05:14:05 +0000538 if (PT == 0)
Chris Lattner38dbdb22007-07-21 03:03:59 +0000539 return Diag(Fn->getLocStart(), diag::err_typecheck_call_not_function,
540 SourceRange(Fn->getLocStart(), RParenLoc));
Chris Lattner3343f812007-06-06 05:05:41 +0000541
Chris Lattnere5a6cbd2007-07-31 21:27:01 +0000542 const FunctionType *funcT = PT->getPointeeType()->getAsFunctionType();
Chris Lattner3d01e4e2007-06-06 05:14:05 +0000543 if (funcT == 0)
Chris Lattner38dbdb22007-07-21 03:03:59 +0000544 return Diag(Fn->getLocStart(), diag::err_typecheck_call_not_function,
545 SourceRange(Fn->getLocStart(), RParenLoc));
Steve Naroffb8c289d2007-05-08 22:18:00 +0000546
Steve Naroff17f76e02007-05-03 21:03:48 +0000547 // If a prototype isn't declared, the parser implicitly defines a func decl
548 QualType resultType = funcT->getResultType();
549
550 if (const FunctionTypeProto *proto = dyn_cast<FunctionTypeProto>(funcT)) {
551 // C99 6.5.2.2p7 - the arguments are implicitly converted, as if by
552 // assignment, to the types of the corresponding parameter, ...
553
554 unsigned NumArgsInProto = proto->getNumArgs();
Steve Naroffb8c289d2007-05-08 22:18:00 +0000555 unsigned NumArgsToCheck = NumArgsInCall;
Steve Naroff17f76e02007-05-03 21:03:48 +0000556
Steve Naroffb8c289d2007-05-08 22:18:00 +0000557 if (NumArgsInCall < NumArgsInProto)
Steve Naroff56faab22007-05-30 04:20:12 +0000558 Diag(RParenLoc, diag::err_typecheck_call_too_few_args,
Chris Lattner38dbdb22007-07-21 03:03:59 +0000559 Fn->getSourceRange());
Steve Naroffb8c289d2007-05-08 22:18:00 +0000560 else if (NumArgsInCall > NumArgsInProto) {
Steve Naroff8563f652007-05-28 19:25:56 +0000561 if (!proto->isVariadic()) {
Chris Lattnera6f5ab52007-07-21 03:09:58 +0000562 Diag(Args[NumArgsInProto]->getLocStart(),
Chris Lattner38dbdb22007-07-21 03:03:59 +0000563 diag::err_typecheck_call_too_many_args, Fn->getSourceRange(),
Chris Lattnera6f5ab52007-07-21 03:09:58 +0000564 SourceRange(Args[NumArgsInProto]->getLocStart(),
565 Args[NumArgsInCall-1]->getLocEnd()));
Steve Naroff8563f652007-05-28 19:25:56 +0000566 }
Steve Naroffb8c289d2007-05-08 22:18:00 +0000567 NumArgsToCheck = NumArgsInProto;
Steve Naroff17f76e02007-05-03 21:03:48 +0000568 }
569 // Continue to check argument types (even if we have too few/many args).
Steve Naroffb8c289d2007-05-08 22:18:00 +0000570 for (unsigned i = 0; i < NumArgsToCheck; i++) {
Chris Lattner38dbdb22007-07-21 03:03:59 +0000571 Expr *argExpr = Args[i];
Steve Naroff83895f72007-09-16 03:34:24 +0000572 assert(argExpr && "ActOnCallExpr(): missing argument expression");
Steve Naroff8563f652007-05-28 19:25:56 +0000573
Steve Naroff17f76e02007-05-03 21:03:48 +0000574 QualType lhsType = proto->getArgType(i);
Steve Naroff8563f652007-05-28 19:25:56 +0000575 QualType rhsType = argExpr->getType();
Steve Naroff44fd8ff2007-07-24 21:46:40 +0000576
Steve Naroffb8af1c22007-07-25 20:45:33 +0000577 // If necessary, apply function/array conversion. C99 6.7.5.3p[7,8].
Chris Lattner41977962007-07-31 19:29:30 +0000578 if (const ArrayType *ary = lhsType->getAsArrayType())
Steve Naroff44fd8ff2007-07-24 21:46:40 +0000579 lhsType = Context.getPointerType(ary->getElementType());
Steve Naroffb8af1c22007-07-25 20:45:33 +0000580 else if (lhsType->isFunctionType())
Steve Naroff44fd8ff2007-07-24 21:46:40 +0000581 lhsType = Context.getPointerType(lhsType);
582
Steve Naroffb8ea4fb2007-07-13 23:32:42 +0000583 AssignmentCheckResult result = CheckSingleAssignmentConstraints(lhsType,
584 argExpr);
Steve Naroff0c1c7ed2007-08-24 22:33:52 +0000585 if (Args[i] != argExpr) // The expression was converted.
586 Args[i] = argExpr; // Make sure we store the converted expression.
Steve Naroff6f49f5d2007-05-29 14:23:36 +0000587 SourceLocation l = argExpr->getLocStart();
Steve Naroff17f76e02007-05-03 21:03:48 +0000588
589 // decode the result (notice that AST's are still created for extensions).
Steve Naroff17f76e02007-05-03 21:03:48 +0000590 switch (result) {
591 case Compatible:
592 break;
593 case PointerFromInt:
Steve Naroff218bc2b2007-05-04 21:54:46 +0000594 // check for null pointer constant (C99 6.3.2.3p3)
Chris Lattner0e9d6222007-07-15 23:26:56 +0000595 if (!argExpr->isNullPointerConstant(Context)) {
Steve Naroff56faab22007-05-30 04:20:12 +0000596 Diag(l, diag::ext_typecheck_passing_pointer_int,
597 lhsType.getAsString(), rhsType.getAsString(),
Chris Lattner38dbdb22007-07-21 03:03:59 +0000598 Fn->getSourceRange(), argExpr->getSourceRange());
Steve Naroffeb9da942007-05-30 00:06:37 +0000599 }
Steve Naroff17f76e02007-05-03 21:03:48 +0000600 break;
601 case IntFromPointer:
Steve Naroff56faab22007-05-30 04:20:12 +0000602 Diag(l, diag::ext_typecheck_passing_pointer_int,
603 lhsType.getAsString(), rhsType.getAsString(),
Chris Lattner38dbdb22007-07-21 03:03:59 +0000604 Fn->getSourceRange(), argExpr->getSourceRange());
Steve Naroff17f76e02007-05-03 21:03:48 +0000605 break;
606 case IncompatiblePointer:
Steve Naroff8563f652007-05-28 19:25:56 +0000607 Diag(l, diag::ext_typecheck_passing_incompatible_pointer,
Steve Naroffeb9da942007-05-30 00:06:37 +0000608 rhsType.getAsString(), lhsType.getAsString(),
Chris Lattner38dbdb22007-07-21 03:03:59 +0000609 Fn->getSourceRange(), argExpr->getSourceRange());
Steve Naroff17f76e02007-05-03 21:03:48 +0000610 break;
Steve Naroff1f4d7272007-05-11 04:00:31 +0000611 case CompatiblePointerDiscardsQualifiers:
Steve Naroffeb9da942007-05-30 00:06:37 +0000612 Diag(l, diag::ext_typecheck_passing_discards_qualifiers,
613 rhsType.getAsString(), lhsType.getAsString(),
Chris Lattner38dbdb22007-07-21 03:03:59 +0000614 Fn->getSourceRange(), argExpr->getSourceRange());
Steve Naroff1f4d7272007-05-11 04:00:31 +0000615 break;
Steve Naroff17f76e02007-05-03 21:03:48 +0000616 case Incompatible:
Steve Naroff8563f652007-05-28 19:25:56 +0000617 return Diag(l, diag::err_typecheck_passing_incompatible,
618 rhsType.getAsString(), lhsType.getAsString(),
Chris Lattner38dbdb22007-07-21 03:03:59 +0000619 Fn->getSourceRange(), argExpr->getSourceRange());
Steve Naroff17f76e02007-05-03 21:03:48 +0000620 }
621 }
Steve Naroff0b661582007-08-28 23:30:39 +0000622 if (NumArgsInCall > NumArgsInProto && proto->isVariadic()) {
623 // Promote the arguments (C99 6.5.2.2p7).
624 for (unsigned i = NumArgsInProto; i < NumArgsInCall; i++) {
625 Expr *argExpr = Args[i];
Steve Naroff83895f72007-09-16 03:34:24 +0000626 assert(argExpr && "ActOnCallExpr(): missing argument expression");
Steve Naroff0b661582007-08-28 23:30:39 +0000627
628 DefaultArgumentPromotion(argExpr);
629 if (Args[i] != argExpr) // The expression was converted.
630 Args[i] = argExpr; // Make sure we store the converted expression.
631 }
632 } else if (NumArgsInCall != NumArgsInProto && !proto->isVariadic()) {
633 // Even if the types checked, bail if the number of arguments don't match.
Steve Naroffb8c289d2007-05-08 22:18:00 +0000634 return true;
Steve Naroff0b661582007-08-28 23:30:39 +0000635 }
636 } else if (isa<FunctionTypeNoProto>(funcT)) {
637 // Promote the arguments (C99 6.5.2.2p6).
638 for (unsigned i = 0; i < NumArgsInCall; i++) {
639 Expr *argExpr = Args[i];
Steve Naroff83895f72007-09-16 03:34:24 +0000640 assert(argExpr && "ActOnCallExpr(): missing argument expression");
Steve Naroff0b661582007-08-28 23:30:39 +0000641
642 DefaultArgumentPromotion(argExpr);
643 if (Args[i] != argExpr) // The expression was converted.
644 Args[i] = argExpr; // Make sure we store the converted expression.
645 }
Steve Naroffae4143e2007-04-26 20:39:23 +0000646 }
Chris Lattnerb87b1b32007-08-10 20:18:51 +0000647 // Do special checking on direct calls to functions.
648 if (ImplicitCastExpr *IcExpr = dyn_cast<ImplicitCastExpr>(Fn))
649 if (DeclRefExpr *DRExpr = dyn_cast<DeclRefExpr>(IcExpr->getSubExpr()))
650 if (FunctionDecl *FDecl = dyn_cast<FunctionDecl>(DRExpr->getDecl()))
Chris Lattnerf17bd422007-08-30 17:45:32 +0000651 if (CheckFunctionCall(Fn, LParenLoc, RParenLoc, FDecl, Args,
652 NumArgsInCall))
Anders Carlsson98f07902007-08-17 05:31:46 +0000653 return true;
Chris Lattnerb87b1b32007-08-10 20:18:51 +0000654
Chris Lattner38dbdb22007-07-21 03:03:59 +0000655 return new CallExpr(Fn, Args, NumArgsInCall, resultType, RParenLoc);
Chris Lattnere168f762006-11-10 05:29:30 +0000656}
657
658Action::ExprResult Sema::
Steve Naroff83895f72007-09-16 03:34:24 +0000659ActOnCompoundLiteral(SourceLocation LParenLoc, TypeTy *Ty,
Steve Naroff57eb2c52007-07-19 21:32:11 +0000660 SourceLocation RParenLoc, ExprTy *InitExpr) {
Steve Naroff83895f72007-09-16 03:34:24 +0000661 assert((Ty != 0) && "ActOnCompoundLiteral(): missing type");
Steve Narofffbd09832007-07-19 01:06:55 +0000662 QualType literalType = QualType::getFromOpaquePtr(Ty);
Steve Naroff57eb2c52007-07-19 21:32:11 +0000663 // FIXME: put back this assert when initializers are worked out.
Steve Naroff83895f72007-09-16 03:34:24 +0000664 //assert((InitExpr != 0) && "ActOnCompoundLiteral(): missing expression");
Steve Naroff57eb2c52007-07-19 21:32:11 +0000665 Expr *literalExpr = static_cast<Expr*>(InitExpr);
Steve Narofffbd09832007-07-19 01:06:55 +0000666
667 // FIXME: add semantic analysis (C99 6.5.2.5).
Steve Naroff57eb2c52007-07-19 21:32:11 +0000668 return new CompoundLiteralExpr(literalType, literalExpr);
Steve Narofffbd09832007-07-19 01:06:55 +0000669}
670
671Action::ExprResult Sema::
Steve Naroff83895f72007-09-16 03:34:24 +0000672ActOnInitList(SourceLocation LBraceLoc, ExprTy **initlist, unsigned NumInit,
Anders Carlsson4692db02007-08-31 04:56:16 +0000673 SourceLocation RBraceLoc) {
Steve Naroff2fea1392007-09-02 02:04:30 +0000674 Expr **InitList = reinterpret_cast<Expr**>(initlist);
Anders Carlsson4692db02007-08-31 04:56:16 +0000675
Steve Naroff30d242c2007-09-15 18:49:24 +0000676 // Semantic analysis for initializers is done by ActOnDeclarator() and
Steve Naroff7d2c5ed2007-09-03 01:24:23 +0000677 // CheckInitializer() - it requires knowledge of the object being intialized.
Anders Carlsson4692db02007-08-31 04:56:16 +0000678
Steve Naroffb03f5942007-09-02 20:30:18 +0000679 InitListExpr *e = new InitListExpr(LBraceLoc, InitList, NumInit, RBraceLoc);
680 e->setType(Context.VoidTy); // FIXME: just a place holder for now.
681 return e;
Steve Narofffbd09832007-07-19 01:06:55 +0000682}
683
684Action::ExprResult Sema::
Steve Naroff83895f72007-09-16 03:34:24 +0000685ActOnCastExpr(SourceLocation LParenLoc, TypeTy *Ty,
Chris Lattnere168f762006-11-10 05:29:30 +0000686 SourceLocation RParenLoc, ExprTy *Op) {
Steve Naroff83895f72007-09-16 03:34:24 +0000687 assert((Ty != 0) && (Op != 0) && "ActOnCastExpr(): missing type or expr");
Steve Naroff1a2cf6b2007-07-16 23:25:18 +0000688
689 Expr *castExpr = static_cast<Expr*>(Op);
690 QualType castType = QualType::getFromOpaquePtr(Ty);
691
Steve Naroff43b8f7f2007-08-31 00:32:44 +0000692 UsualUnaryConversions(castExpr);
693
Chris Lattnerbd270732007-07-18 16:00:06 +0000694 // C99 6.5.4p2: the cast type needs to be void or scalar and the expression
695 // type needs to be scalar.
Chris Lattner3bc4d202007-10-29 04:26:44 +0000696 if (!castType->isVoidType()) { // Cast to void allows any expr type.
697 if (!castType->isScalarType())
698 return Diag(LParenLoc, diag::err_typecheck_cond_expect_scalar,
699 castType.getAsString(), SourceRange(LParenLoc, RParenLoc));
700 if (!castExpr->getType()->isScalarType()) {
701 return Diag(castExpr->getLocStart(),
702 diag::err_typecheck_expect_scalar_operand,
703 castExpr->getType().getAsString(),castExpr->getSourceRange());
704 }
Steve Naroff1a2cf6b2007-07-16 23:25:18 +0000705 }
706 return new CastExpr(castType, castExpr, LParenLoc);
Chris Lattnere168f762006-11-10 05:29:30 +0000707}
708
Steve Naroffa78c4642007-10-18 05:13:08 +0000709// promoteExprToType - a helper function to ensure we create exactly one
710// ImplicitCastExpr.
711static void promoteExprToType(Expr *&expr, QualType type) {
712 if (ImplicitCastExpr *impCast = dyn_cast<ImplicitCastExpr>(expr))
713 impCast->setType(type);
714 else
715 expr = new ImplicitCastExpr(type, expr);
716 return;
717}
718
Steve Narofff8a28c52007-05-15 20:29:32 +0000719inline QualType Sema::CheckConditionalOperands( // C99 6.5.15
Steve Naroff7a5af782007-07-13 16:58:59 +0000720 Expr *&cond, Expr *&lex, Expr *&rex, SourceLocation questionLoc) {
Steve Naroff31090012007-07-16 21:54:35 +0000721 UsualUnaryConversions(cond);
722 UsualUnaryConversions(lex);
723 UsualUnaryConversions(rex);
724 QualType condT = cond->getType();
725 QualType lexT = lex->getType();
726 QualType rexT = rex->getType();
727
Steve Naroffa78fe7e2007-05-16 19:47:19 +0000728 // first, check the condition.
Steve Naroff7a5af782007-07-13 16:58:59 +0000729 if (!condT->isScalarType()) { // C99 6.5.15p2
730 Diag(cond->getLocStart(), diag::err_typecheck_cond_expect_scalar,
731 condT.getAsString());
Steve Naroffa78fe7e2007-05-16 19:47:19 +0000732 return QualType();
733 }
734 // now check the two expressions.
Steve Naroffdbd9e892007-07-17 00:58:39 +0000735 if (lexT->isArithmeticType() && rexT->isArithmeticType()) { // C99 6.5.15p3,5
736 UsualArithmeticConversions(lex, rex);
737 return lex->getType();
738 }
Chris Lattnere5a6cbd2007-07-31 21:27:01 +0000739 if (const RecordType *LHSRT = lexT->getAsRecordType()) { // C99 6.5.15p3
740 if (const RecordType *RHSRT = rexT->getAsRecordType()) {
741
Chris Lattnerf17bd422007-08-30 17:45:32 +0000742 if (LHSRT->getDecl()->getIdentifier() ==RHSRT->getDecl()->getIdentifier())
Chris Lattnere5a6cbd2007-07-31 21:27:01 +0000743 return lexT;
744
Steve Naroffa78fe7e2007-05-16 19:47:19 +0000745 Diag(questionLoc, diag::err_typecheck_cond_incompatible_operands,
Steve Naroff7a5af782007-07-13 16:58:59 +0000746 lexT.getAsString(), rexT.getAsString(),
747 lex->getSourceRange(), rex->getSourceRange());
Steve Naroffa78fe7e2007-05-16 19:47:19 +0000748 return QualType();
749 }
750 }
Chris Lattner0e9d6222007-07-15 23:26:56 +0000751 // C99 6.5.15p3
Steve Naroffa78c4642007-10-18 05:13:08 +0000752 if (lexT->isPointerType() && rex->isNullPointerConstant(Context)) {
753 promoteExprToType(rex, lexT); // promote the null to a pointer.
Steve Naroff7a5af782007-07-13 16:58:59 +0000754 return lexT;
Steve Naroffa78c4642007-10-18 05:13:08 +0000755 }
756 if (rexT->isPointerType() && lex->isNullPointerConstant(Context)) {
757 promoteExprToType(lex, rexT); // promote the null to a pointer.
Steve Naroff7a5af782007-07-13 16:58:59 +0000758 return rexT;
Steve Naroffa78c4642007-10-18 05:13:08 +0000759 }
Chris Lattnere5a6cbd2007-07-31 21:27:01 +0000760 if (const PointerType *LHSPT = lexT->getAsPointerType()) { // C99 6.5.15p3,6
761 if (const PointerType *RHSPT = rexT->getAsPointerType()) {
762 // get the "pointed to" types
763 QualType lhptee = LHSPT->getPointeeType();
764 QualType rhptee = RHSPT->getPointeeType();
Steve Naroffa78fe7e2007-05-16 19:47:19 +0000765
Chris Lattnere5a6cbd2007-07-31 21:27:01 +0000766 // ignore qualifiers on void (C99 6.5.15p3, clause 6)
767 if (lhptee->isVoidType() &&
768 (rhptee->isObjectType() || rhptee->isIncompleteType()))
769 return lexT;
770 if (rhptee->isVoidType() &&
771 (lhptee->isObjectType() || lhptee->isIncompleteType()))
772 return rexT;
Steve Naroffa78fe7e2007-05-16 19:47:19 +0000773
Steve Naroff32e44c02007-10-15 20:41:53 +0000774 if (!Context.typesAreCompatible(lhptee.getUnqualifiedType(),
775 rhptee.getUnqualifiedType())) {
Chris Lattnere5a6cbd2007-07-31 21:27:01 +0000776 Diag(questionLoc, diag::ext_typecheck_cond_incompatible_pointers,
777 lexT.getAsString(), rexT.getAsString(),
778 lex->getSourceRange(), rex->getSourceRange());
779 return lexT; // FIXME: this is an _ext - is this return o.k?
780 }
781 // The pointer types are compatible.
Chris Lattnerf17bd422007-08-30 17:45:32 +0000782 // C99 6.5.15p6: If both operands are pointers to compatible types *or* to
783 // differently qualified versions of compatible types, the result type is
784 // a pointer to an appropriately qualified version of the *composite*
785 // type.
Chris Lattnere5a6cbd2007-07-31 21:27:01 +0000786 return lexT; // FIXME: Need to return the composite type.
Steve Naroffa78fe7e2007-05-16 19:47:19 +0000787 }
788 }
Chris Lattnere5a6cbd2007-07-31 21:27:01 +0000789
Steve Naroff7a5af782007-07-13 16:58:59 +0000790 if (lexT->isVoidType() && rexT->isVoidType()) // C99 6.5.15p3
791 return lexT;
Steve Naroffa78fe7e2007-05-16 19:47:19 +0000792
793 Diag(questionLoc, diag::err_typecheck_cond_incompatible_operands,
Steve Naroff7a5af782007-07-13 16:58:59 +0000794 lexT.getAsString(), rexT.getAsString(),
795 lex->getSourceRange(), rex->getSourceRange());
Steve Naroffa78fe7e2007-05-16 19:47:19 +0000796 return QualType();
Steve Narofff8a28c52007-05-15 20:29:32 +0000797}
798
Steve Naroff83895f72007-09-16 03:34:24 +0000799/// ActOnConditionalOp - Parse a ?: operation. Note that 'LHS' may be null
Chris Lattnere168f762006-11-10 05:29:30 +0000800/// in the case of a the GNU conditional expr extension.
Steve Naroff83895f72007-09-16 03:34:24 +0000801Action::ExprResult Sema::ActOnConditionalOp(SourceLocation QuestionLoc,
Chris Lattnere168f762006-11-10 05:29:30 +0000802 SourceLocation ColonLoc,
803 ExprTy *Cond, ExprTy *LHS,
804 ExprTy *RHS) {
Chris Lattnerdaaa9f22007-07-16 21:39:03 +0000805 Expr *CondExpr = (Expr *) Cond;
806 Expr *LHSExpr = (Expr *) LHS, *RHSExpr = (Expr *) RHS;
807 QualType result = CheckConditionalOperands(CondExpr, LHSExpr,
808 RHSExpr, QuestionLoc);
Steve Narofff8a28c52007-05-15 20:29:32 +0000809 if (result.isNull())
810 return true;
Chris Lattnerdaaa9f22007-07-16 21:39:03 +0000811 return new ConditionalOperator(CondExpr, LHSExpr, RHSExpr, result);
Chris Lattnere168f762006-11-10 05:29:30 +0000812}
813
Steve Naroff0b661582007-08-28 23:30:39 +0000814/// DefaultArgumentPromotion (C99 6.5.2.2p6). Used for function calls that
815/// do not have a prototype. Integer promotions are performed on each
816/// argument, and arguments that have type float are promoted to double.
817void Sema::DefaultArgumentPromotion(Expr *&expr) {
818 QualType t = expr->getType();
819 assert(!t.isNull() && "DefaultArgumentPromotion - missing type");
820
821 if (t->isPromotableIntegerType()) // C99 6.3.1.1p2
822 promoteExprToType(expr, Context.IntTy);
823 if (t == Context.FloatTy)
824 promoteExprToType(expr, Context.DoubleTy);
825}
826
Steve Naroff81569d22007-07-15 02:02:06 +0000827/// DefaultFunctionArrayConversion (C99 6.3.2.1p3, C99 6.3.2.1p4).
Steve Naroff31090012007-07-16 21:54:35 +0000828void Sema::DefaultFunctionArrayConversion(Expr *&e) {
Steve Naroff81569d22007-07-15 02:02:06 +0000829 QualType t = e->getType();
Steve Naroffb8ea4fb2007-07-13 23:32:42 +0000830 assert(!t.isNull() && "DefaultFunctionArrayConversion - missing type");
Bill Wendlingdfc81072007-07-17 03:52:31 +0000831
Chris Lattnercd1d0862007-07-31 16:56:34 +0000832 if (const ReferenceType *ref = t->getAsReferenceType()) {
Bill Wendling354fb262007-07-17 04:16:47 +0000833 promoteExprToType(e, ref->getReferenceeType()); // C++ [expr]
834 t = e->getType();
835 }
Steve Naroff81569d22007-07-15 02:02:06 +0000836 if (t->isFunctionType())
Steve Naroff31090012007-07-16 21:54:35 +0000837 promoteExprToType(e, Context.getPointerType(t));
Chris Lattner41977962007-07-31 19:29:30 +0000838 else if (const ArrayType *ary = t->getAsArrayType())
Steve Naroff31090012007-07-16 21:54:35 +0000839 promoteExprToType(e, Context.getPointerType(ary->getElementType()));
Steve Naroff1926c832007-04-24 00:23:05 +0000840}
841
Steve Naroff71b59a92007-06-04 22:22:31 +0000842/// UsualUnaryConversion - Performs various conversions that are common to most
843/// operators (C99 6.3). The conversions of array and function types are
844/// sometimes surpressed. For example, the array->pointer conversion doesn't
845/// apply if the array is an argument to the sizeof or address (&) operators.
846/// In these instances, this routine should *not* be called.
Steve Naroff31090012007-07-16 21:54:35 +0000847void Sema::UsualUnaryConversions(Expr *&expr) {
Steve Naroff7a5af782007-07-13 16:58:59 +0000848 QualType t = expr->getType();
Steve Naroff71b59a92007-06-04 22:22:31 +0000849 assert(!t.isNull() && "UsualUnaryConversions - missing type");
850
Chris Lattnercd1d0862007-07-31 16:56:34 +0000851 if (const ReferenceType *ref = t->getAsReferenceType()) {
Bill Wendling354fb262007-07-17 04:16:47 +0000852 promoteExprToType(expr, ref->getReferenceeType()); // C++ [expr]
853 t = expr->getType();
854 }
Steve Naroff81569d22007-07-15 02:02:06 +0000855 if (t->isPromotableIntegerType()) // C99 6.3.1.1p2
Steve Naroff31090012007-07-16 21:54:35 +0000856 promoteExprToType(expr, Context.IntTy);
857 else
858 DefaultFunctionArrayConversion(expr);
Steve Naroff71b59a92007-06-04 22:22:31 +0000859}
860
Chris Lattnerf17bd422007-08-30 17:45:32 +0000861/// UsualArithmeticConversions - Performs various conversions that are common to
Steve Naroff1926c832007-04-24 00:23:05 +0000862/// binary operators (C99 6.3.1.8). If both operands aren't arithmetic, this
863/// routine returns the first non-arithmetic type found. The client is
864/// responsible for emitting appropriate error diagnostics.
Steve Naroffbe4c4d12007-08-24 19:07:16 +0000865QualType Sema::UsualArithmeticConversions(Expr *&lhsExpr, Expr *&rhsExpr,
866 bool isCompAssign) {
Steve Naroff46c72912007-08-25 19:54:59 +0000867 if (!isCompAssign) {
868 UsualUnaryConversions(lhsExpr);
869 UsualUnaryConversions(rhsExpr);
870 }
Steve Naroff1bb21df2007-10-18 18:55:53 +0000871 // For conversion purposes, we ignore any qualifiers.
872 // For example, "const float" and "float" are equivalent.
Steve Naroff257b4a22007-11-10 19:45:54 +0000873 QualType lhs = lhsExpr->getType().getCanonicalType().getUnqualifiedType();
874 QualType rhs = rhsExpr->getType().getCanonicalType().getUnqualifiedType();
Steve Naroff1926c832007-04-24 00:23:05 +0000875
Chris Lattner0c46c5d2007-06-02 23:53:17 +0000876 // If both types are identical, no conversion is needed.
Steve Naroff1bb21df2007-10-18 18:55:53 +0000877 if (lhs == rhs)
878 return lhs;
Chris Lattner0c46c5d2007-06-02 23:53:17 +0000879
880 // If either side is a non-arithmetic type (e.g. a pointer), we are done.
881 // The caller can deal with this (e.g. pointer + int).
Steve Naroffdbd9e892007-07-17 00:58:39 +0000882 if (!lhs->isArithmeticType() || !rhs->isArithmeticType())
Steve Naroffbe4c4d12007-08-24 19:07:16 +0000883 return lhs;
Steve Naroff1926c832007-04-24 00:23:05 +0000884
Chris Lattner0c46c5d2007-06-02 23:53:17 +0000885 // At this point, we have two different arithmetic types.
Steve Naroffb01bbe32007-04-25 01:22:31 +0000886
887 // Handle complex types first (C99 6.3.1.8p1).
888 if (lhs->isComplexType() || rhs->isComplexType()) {
889 // if we have an integer operand, the result is the complex type.
Steve Naroffdbd9e892007-07-17 00:58:39 +0000890 if (rhs->isIntegerType()) { // convert the rhs to the lhs complex type.
Steve Naroffbe4c4d12007-08-24 19:07:16 +0000891 if (!isCompAssign) promoteExprToType(rhsExpr, lhs);
892 return lhs;
Steve Naroffdbd9e892007-07-17 00:58:39 +0000893 }
894 if (lhs->isIntegerType()) { // convert the lhs to the rhs complex type.
Steve Naroffbe4c4d12007-08-24 19:07:16 +0000895 if (!isCompAssign) promoteExprToType(lhsExpr, rhs);
896 return rhs;
Steve Naroffdbd9e892007-07-17 00:58:39 +0000897 }
Steve Naroff9091ef72007-08-27 01:27:54 +0000898 // This handles complex/complex, complex/float, or float/complex.
899 // When both operands are complex, the shorter operand is converted to the
900 // type of the longer, and that is the type of the result. This corresponds
901 // to what is done when combining two real floating-point operands.
902 // The fun begins when size promotion occur across type domains.
903 // From H&S 6.3.4: When one operand is complex and the other is a real
904 // floating-point type, the less precise type is converted, within it's
905 // real or complex domain, to the precision of the other type. For example,
906 // when combining a "long double" with a "double _Complex", the
907 // "double _Complex" is promoted to "long double _Complex".
Steve Naroff7af82d42007-08-27 15:30:22 +0000908 int result = Context.compareFloatingType(lhs, rhs);
909
910 if (result > 0) { // The left side is bigger, convert rhs.
Steve Naroffe31313d2007-08-27 21:32:55 +0000911 rhs = Context.getFloatingTypeOfSizeWithinDomain(lhs, rhs);
912 if (!isCompAssign)
913 promoteExprToType(rhsExpr, rhs);
914 } else if (result < 0) { // The right side is bigger, convert lhs.
915 lhs = Context.getFloatingTypeOfSizeWithinDomain(rhs, lhs);
916 if (!isCompAssign)
917 promoteExprToType(lhsExpr, lhs);
918 }
919 // At this point, lhs and rhs have the same rank/size. Now, make sure the
920 // domains match. This is a requirement for our implementation, C99
921 // does not require this promotion.
922 if (lhs != rhs) { // Domains don't match, we have complex/float mix.
923 if (lhs->isRealFloatingType()) { // handle "double, _Complex double".
Steve Naroffa042db22007-08-27 21:43:43 +0000924 if (!isCompAssign)
925 promoteExprToType(lhsExpr, rhs);
926 return rhs;
Steve Naroffe31313d2007-08-27 21:32:55 +0000927 } else { // handle "_Complex double, double".
Steve Naroffa042db22007-08-27 21:43:43 +0000928 if (!isCompAssign)
929 promoteExprToType(rhsExpr, lhs);
930 return lhs;
Steve Naroffe31313d2007-08-27 21:32:55 +0000931 }
Steve Naroffdbd9e892007-07-17 00:58:39 +0000932 }
Steve Naroffa042db22007-08-27 21:43:43 +0000933 return lhs; // The domain/size match exactly.
Steve Naroffbf223ba2007-04-24 20:56:26 +0000934 }
Steve Naroffb01bbe32007-04-25 01:22:31 +0000935 // Now handle "real" floating types (i.e. float, double, long double).
936 if (lhs->isRealFloatingType() || rhs->isRealFloatingType()) {
937 // if we have an integer operand, the result is the real floating type.
Steve Naroffdbd9e892007-07-17 00:58:39 +0000938 if (rhs->isIntegerType()) { // convert rhs to the lhs floating point type.
Steve Naroffbe4c4d12007-08-24 19:07:16 +0000939 if (!isCompAssign) promoteExprToType(rhsExpr, lhs);
940 return lhs;
Steve Naroffdbd9e892007-07-17 00:58:39 +0000941 }
942 if (lhs->isIntegerType()) { // convert lhs to the rhs floating point type.
Steve Naroffbe4c4d12007-08-24 19:07:16 +0000943 if (!isCompAssign) promoteExprToType(lhsExpr, rhs);
944 return rhs;
Steve Naroffdbd9e892007-07-17 00:58:39 +0000945 }
Steve Naroff81569d22007-07-15 02:02:06 +0000946 // We have two real floating types, float/complex combos were handled above.
947 // Convert the smaller operand to the bigger result.
Steve Naroff7af82d42007-08-27 15:30:22 +0000948 int result = Context.compareFloatingType(lhs, rhs);
949
950 if (result > 0) { // convert the rhs
Steve Naroffbe4c4d12007-08-24 19:07:16 +0000951 if (!isCompAssign) promoteExprToType(rhsExpr, lhs);
952 return lhs;
Steve Naroffdbd9e892007-07-17 00:58:39 +0000953 }
Steve Naroff7af82d42007-08-27 15:30:22 +0000954 if (result < 0) { // convert the lhs
955 if (!isCompAssign) promoteExprToType(lhsExpr, rhs); // convert the lhs
956 return rhs;
957 }
958 assert(0 && "Sema::UsualArithmeticConversions(): illegal float comparison");
Steve Naroffb01bbe32007-04-25 01:22:31 +0000959 }
Steve Naroff81569d22007-07-15 02:02:06 +0000960 // Finally, we have two differing integer types.
Steve Naroffdbd9e892007-07-17 00:58:39 +0000961 if (Context.maxIntegerType(lhs, rhs) == lhs) { // convert the rhs
Steve Naroffbe4c4d12007-08-24 19:07:16 +0000962 if (!isCompAssign) promoteExprToType(rhsExpr, lhs);
963 return lhs;
Steve Naroffdbd9e892007-07-17 00:58:39 +0000964 }
Steve Naroffbe4c4d12007-08-24 19:07:16 +0000965 if (!isCompAssign) promoteExprToType(lhsExpr, rhs); // convert the lhs
966 return rhs;
Steve Narofff1e53692007-03-23 22:27:02 +0000967}
968
Steve Naroff3f597292007-05-11 22:18:03 +0000969// CheckPointerTypesForAssignment - This is a very tricky routine (despite
970// being closely modeled after the C99 spec:-). The odd characteristic of this
971// routine is it effectively iqnores the qualifiers on the top level pointee.
972// This circumvents the usual type rules specified in 6.2.7p1 & 6.7.5.[1-3].
973// FIXME: add a couple examples in this comment.
Steve Naroff98cf3e92007-06-06 18:38:38 +0000974Sema::AssignmentCheckResult
975Sema::CheckPointerTypesForAssignment(QualType lhsType, QualType rhsType) {
Steve Naroff3f597292007-05-11 22:18:03 +0000976 QualType lhptee, rhptee;
Steve Naroff1f4d7272007-05-11 04:00:31 +0000977
978 // get the "pointed to" type (ignoring qualifiers at the top level)
Chris Lattnere5a6cbd2007-07-31 21:27:01 +0000979 lhptee = lhsType->getAsPointerType()->getPointeeType();
980 rhptee = rhsType->getAsPointerType()->getPointeeType();
Steve Naroff1f4d7272007-05-11 04:00:31 +0000981
982 // make sure we operate on the canonical type
Steve Naroff3f597292007-05-11 22:18:03 +0000983 lhptee = lhptee.getCanonicalType();
984 rhptee = rhptee.getCanonicalType();
Steve Naroff1f4d7272007-05-11 04:00:31 +0000985
Steve Naroff98cf3e92007-06-06 18:38:38 +0000986 AssignmentCheckResult r = Compatible;
987
Steve Naroff3f597292007-05-11 22:18:03 +0000988 // C99 6.5.16.1p1: This following citation is common to constraints
989 // 3 & 4 (below). ...and the type *pointed to* by the left has all the
990 // qualifiers of the type *pointed to* by the right;
991 if ((lhptee.getQualifiers() & rhptee.getQualifiers()) !=
992 rhptee.getQualifiers())
993 r = CompatiblePointerDiscardsQualifiers;
994
995 // C99 6.5.16.1p1 (constraint 4): If one operand is a pointer to an object or
996 // incomplete type and the other is a pointer to a qualified or unqualified
997 // version of void...
998 if (lhptee.getUnqualifiedType()->isVoidType() &&
999 (rhptee->isObjectType() || rhptee->isIncompleteType()))
1000 ;
1001 else if (rhptee.getUnqualifiedType()->isVoidType() &&
1002 (lhptee->isObjectType() || lhptee->isIncompleteType()))
1003 ;
1004 // C99 6.5.16.1p1 (constraint 3): both operands are pointers to qualified or
1005 // unqualified versions of compatible types, ...
Steve Naroff32e44c02007-10-15 20:41:53 +00001006 else if (!Context.typesAreCompatible(lhptee.getUnqualifiedType(),
1007 rhptee.getUnqualifiedType()))
Steve Naroff3f597292007-05-11 22:18:03 +00001008 r = IncompatiblePointer; // this "trumps" PointerAssignDiscardsQualifiers
Steve Naroff98cf3e92007-06-06 18:38:38 +00001009 return r;
Steve Naroff1f4d7272007-05-11 04:00:31 +00001010}
1011
Steve Naroff98cf3e92007-06-06 18:38:38 +00001012/// CheckAssignmentConstraints (C99 6.5.16) - This routine currently
Steve Naroff17f76e02007-05-03 21:03:48 +00001013/// has code to accommodate several GCC extensions when type checking
1014/// pointers. Here are some objectionable examples that GCC considers warnings:
1015///
1016/// int a, *pint;
1017/// short *pshort;
1018/// struct foo *pfoo;
1019///
1020/// pint = pshort; // warning: assignment from incompatible pointer type
1021/// a = pint; // warning: assignment makes integer from pointer without a cast
1022/// pint = a; // warning: assignment makes pointer from integer without a cast
1023/// pint = pfoo; // warning: assignment from incompatible pointer type
1024///
1025/// As a result, the code for dealing with pointers is more complex than the
1026/// C99 spec dictates.
1027/// Note: the warning above turn into errors when -pedantic-errors is enabled.
1028///
Steve Naroff98cf3e92007-06-06 18:38:38 +00001029Sema::AssignmentCheckResult
1030Sema::CheckAssignmentConstraints(QualType lhsType, QualType rhsType) {
Chris Lattnerb104d312007-10-29 05:15:40 +00001031 if (lhsType.getCanonicalType() == rhsType.getCanonicalType())
1032 return Compatible; // common case, fast path...
Steve Naroff44fd8ff2007-07-24 21:46:40 +00001033
Anders Carlsson24ebce62007-10-12 23:56:29 +00001034 if (lhsType->isReferenceType() || rhsType->isReferenceType()) {
Steve Naroff32e44c02007-10-15 20:41:53 +00001035 if (Context.referenceTypesAreCompatible(lhsType, rhsType))
Anders Carlsson24ebce62007-10-12 23:56:29 +00001036 return Compatible;
1037 } else if (lhsType->isArithmeticType() && rhsType->isArithmeticType()) {
Steve Naroffe728ba32007-07-10 22:20:04 +00001038 if (lhsType->isVectorType() || rhsType->isVectorType()) {
1039 if (lhsType.getCanonicalType() != rhsType.getCanonicalType())
1040 return Incompatible;
1041 }
Steve Naroff98cf3e92007-06-06 18:38:38 +00001042 return Compatible;
Steve Naroff84ff4b42007-07-09 21:31:10 +00001043 } else if (lhsType->isPointerType()) {
Steve Naroff98cf3e92007-06-06 18:38:38 +00001044 if (rhsType->isIntegerType())
1045 return PointerFromInt;
1046
Steve Naroff3f597292007-05-11 22:18:03 +00001047 if (rhsType->isPointerType())
Steve Naroff98cf3e92007-06-06 18:38:38 +00001048 return CheckPointerTypesForAssignment(lhsType, rhsType);
Steve Naroff9eb24652007-05-02 21:58:15 +00001049 } else if (rhsType->isPointerType()) {
Steve Naroff98cf3e92007-06-06 18:38:38 +00001050 // C99 6.5.16.1p1: the left operand is _Bool and the right is a pointer.
1051 if ((lhsType->isIntegerType()) && (lhsType != Context.BoolTy))
1052 return IntFromPointer;
1053
Steve Naroff3f597292007-05-11 22:18:03 +00001054 if (lhsType->isPointerType())
Steve Naroff98cf3e92007-06-06 18:38:38 +00001055 return CheckPointerTypesForAssignment(lhsType, rhsType);
Steve Naroff1f4d7272007-05-11 04:00:31 +00001056 } else if (isa<TagType>(lhsType) && isa<TagType>(rhsType)) {
Steve Naroff32e44c02007-10-15 20:41:53 +00001057 if (Context.tagTypesAreCompatible(lhsType, rhsType))
Steve Naroff98cf3e92007-06-06 18:38:38 +00001058 return Compatible;
Bill Wendling216423b2007-05-30 06:30:29 +00001059 }
Steve Naroff98cf3e92007-06-06 18:38:38 +00001060 return Incompatible;
Steve Naroff9eb24652007-05-02 21:58:15 +00001061}
1062
Steve Naroffb8ea4fb2007-07-13 23:32:42 +00001063Sema::AssignmentCheckResult
1064Sema::CheckSingleAssignmentConstraints(QualType lhsType, Expr *&rExpr) {
Chris Lattnere6dcd502007-10-16 02:55:40 +00001065 // This check seems unnatural, however it is necessary to ensure the proper
Steve Naroffb8ea4fb2007-07-13 23:32:42 +00001066 // conversion of functions/arrays. If the conversion were done for all
Steve Naroff30d242c2007-09-15 18:49:24 +00001067 // DeclExpr's (created by ActOnIdentifierExpr), it would mess up the unary
Steve Naroffb8ea4fb2007-07-13 23:32:42 +00001068 // expressions that surpress this implicit conversion (&, sizeof).
Chris Lattnere6dcd502007-10-16 02:55:40 +00001069 //
1070 // Suppress this for references: C99 8.5.3p5. FIXME: revisit when references
1071 // are better understood.
1072 if (!lhsType->isReferenceType())
1073 DefaultFunctionArrayConversion(rExpr);
Steve Naroff0c1c7ed2007-08-24 22:33:52 +00001074
1075 Sema::AssignmentCheckResult result;
Steve Naroffb8ea4fb2007-07-13 23:32:42 +00001076
Steve Naroff0c1c7ed2007-08-24 22:33:52 +00001077 result = CheckAssignmentConstraints(lhsType, rExpr->getType());
1078
1079 // C99 6.5.16.1p2: The value of the right operand is converted to the
1080 // type of the assignment expression.
1081 if (rExpr->getType() != lhsType)
1082 promoteExprToType(rExpr, lhsType);
1083 return result;
Steve Naroffb8ea4fb2007-07-13 23:32:42 +00001084}
1085
1086Sema::AssignmentCheckResult
1087Sema::CheckCompoundAssignmentConstraints(QualType lhsType, QualType rhsType) {
1088 return CheckAssignmentConstraints(lhsType, rhsType);
1089}
1090
Steve Naroff7a5af782007-07-13 16:58:59 +00001091inline void Sema::InvalidOperands(SourceLocation loc, Expr *&lex, Expr *&rex) {
Steve Naroff6f49f5d2007-05-29 14:23:36 +00001092 Diag(loc, diag::err_typecheck_invalid_operands,
1093 lex->getType().getAsString(), rex->getType().getAsString(),
1094 lex->getSourceRange(), rex->getSourceRange());
1095}
1096
Steve Naroff7a5af782007-07-13 16:58:59 +00001097inline QualType Sema::CheckVectorOperands(SourceLocation loc, Expr *&lex,
1098 Expr *&rex) {
Steve Naroff84ff4b42007-07-09 21:31:10 +00001099 QualType lhsType = lex->getType(), rhsType = rex->getType();
1100
1101 // make sure the vector types are identical.
1102 if (lhsType == rhsType)
1103 return lhsType;
1104 // You cannot convert between vector values of different size.
1105 Diag(loc, diag::err_typecheck_vector_not_convertable,
1106 lex->getType().getAsString(), rex->getType().getAsString(),
1107 lex->getSourceRange(), rex->getSourceRange());
1108 return QualType();
1109}
1110
Steve Naroff218bc2b2007-05-04 21:54:46 +00001111inline QualType Sema::CheckMultiplyDivideOperands(
Steve Naroffbe4c4d12007-08-24 19:07:16 +00001112 Expr *&lex, Expr *&rex, SourceLocation loc, bool isCompAssign)
Steve Naroff5c10d4b2007-04-20 23:42:24 +00001113{
Steve Naroffb8ea4fb2007-07-13 23:32:42 +00001114 QualType lhsType = lex->getType(), rhsType = rex->getType();
1115
1116 if (lhsType->isVectorType() || rhsType->isVectorType())
Steve Naroff84ff4b42007-07-09 21:31:10 +00001117 return CheckVectorOperands(loc, lex, rex);
Steve Naroff7a5af782007-07-13 16:58:59 +00001118
Steve Naroffbe4c4d12007-08-24 19:07:16 +00001119 QualType compType = UsualArithmeticConversions(lex, rex, isCompAssign);
Steve Naroff5c10d4b2007-04-20 23:42:24 +00001120
Steve Naroffdbd9e892007-07-17 00:58:39 +00001121 if (lex->getType()->isArithmeticType() && rex->getType()->isArithmeticType())
Steve Naroffbe4c4d12007-08-24 19:07:16 +00001122 return compType;
Steve Naroff6f49f5d2007-05-29 14:23:36 +00001123 InvalidOperands(loc, lex, rex);
Steve Naroff218bc2b2007-05-04 21:54:46 +00001124 return QualType();
Steve Naroff26c8ea52007-03-21 21:08:52 +00001125}
1126
Steve Naroff218bc2b2007-05-04 21:54:46 +00001127inline QualType Sema::CheckRemainderOperands(
Steve Naroffbe4c4d12007-08-24 19:07:16 +00001128 Expr *&lex, Expr *&rex, SourceLocation loc, bool isCompAssign)
Steve Naroff218bc2b2007-05-04 21:54:46 +00001129{
Steve Naroffb8ea4fb2007-07-13 23:32:42 +00001130 QualType lhsType = lex->getType(), rhsType = rex->getType();
1131
Steve Naroffbe4c4d12007-08-24 19:07:16 +00001132 QualType compType = UsualArithmeticConversions(lex, rex, isCompAssign);
Steve Naroff218bc2b2007-05-04 21:54:46 +00001133
Steve Naroffdbd9e892007-07-17 00:58:39 +00001134 if (lex->getType()->isIntegerType() && rex->getType()->isIntegerType())
Steve Naroffbe4c4d12007-08-24 19:07:16 +00001135 return compType;
Steve Naroff6f49f5d2007-05-29 14:23:36 +00001136 InvalidOperands(loc, lex, rex);
Steve Naroff218bc2b2007-05-04 21:54:46 +00001137 return QualType();
1138}
1139
1140inline QualType Sema::CheckAdditionOperands( // C99 6.5.6
Steve Naroffbe4c4d12007-08-24 19:07:16 +00001141 Expr *&lex, Expr *&rex, SourceLocation loc, bool isCompAssign)
Steve Naroff1926c832007-04-24 00:23:05 +00001142{
Steve Naroff94a5aca2007-07-16 22:23:01 +00001143 if (lex->getType()->isVectorType() || rex->getType()->isVectorType())
Steve Naroff7a5af782007-07-13 16:58:59 +00001144 return CheckVectorOperands(loc, lex, rex);
1145
Steve Naroffbe4c4d12007-08-24 19:07:16 +00001146 QualType compType = UsualArithmeticConversions(lex, rex, isCompAssign);
Steve Naroff94a5aca2007-07-16 22:23:01 +00001147
Steve Naroffe4718892007-04-27 18:30:00 +00001148 // handle the common case first (both operands are arithmetic).
Steve Naroffdbd9e892007-07-17 00:58:39 +00001149 if (lex->getType()->isArithmeticType() && rex->getType()->isArithmeticType())
Steve Naroffbe4c4d12007-08-24 19:07:16 +00001150 return compType;
Steve Naroff218bc2b2007-05-04 21:54:46 +00001151
Steve Naroffdbd9e892007-07-17 00:58:39 +00001152 if (lex->getType()->isPointerType() && rex->getType()->isIntegerType())
1153 return lex->getType();
1154 if (lex->getType()->isIntegerType() && rex->getType()->isPointerType())
1155 return rex->getType();
Steve Naroff6f49f5d2007-05-29 14:23:36 +00001156 InvalidOperands(loc, lex, rex);
Steve Naroff218bc2b2007-05-04 21:54:46 +00001157 return QualType();
Steve Naroff26c8ea52007-03-21 21:08:52 +00001158}
1159
Steve Naroff218bc2b2007-05-04 21:54:46 +00001160inline QualType Sema::CheckSubtractionOperands( // C99 6.5.6
Steve Naroffbe4c4d12007-08-24 19:07:16 +00001161 Expr *&lex, Expr *&rex, SourceLocation loc, bool isCompAssign)
Steve Naroff218bc2b2007-05-04 21:54:46 +00001162{
Steve Naroff94a5aca2007-07-16 22:23:01 +00001163 if (lex->getType()->isVectorType() || rex->getType()->isVectorType())
Steve Naroff84ff4b42007-07-09 21:31:10 +00001164 return CheckVectorOperands(loc, lex, rex);
Steve Naroffb8ea4fb2007-07-13 23:32:42 +00001165
Steve Naroffbe4c4d12007-08-24 19:07:16 +00001166 QualType compType = UsualArithmeticConversions(lex, rex, isCompAssign);
Steve Naroff218bc2b2007-05-04 21:54:46 +00001167
1168 // handle the common case first (both operands are arithmetic).
Steve Naroffdbd9e892007-07-17 00:58:39 +00001169 if (lex->getType()->isArithmeticType() && rex->getType()->isArithmeticType())
Steve Naroffbe4c4d12007-08-24 19:07:16 +00001170 return compType;
Steve Naroff94a5aca2007-07-16 22:23:01 +00001171
1172 if (lex->getType()->isPointerType() && rex->getType()->isIntegerType())
Steve Naroffbe4c4d12007-08-24 19:07:16 +00001173 return compType;
Steve Naroff94a5aca2007-07-16 22:23:01 +00001174 if (lex->getType()->isPointerType() && rex->getType()->isPointerType())
Chris Lattnerd2b88ab2007-07-13 03:05:23 +00001175 return Context.getPointerDiffType();
Steve Naroff6f49f5d2007-05-29 14:23:36 +00001176 InvalidOperands(loc, lex, rex);
Steve Naroff218bc2b2007-05-04 21:54:46 +00001177 return QualType();
1178}
1179
1180inline QualType Sema::CheckShiftOperands( // C99 6.5.7
Steve Naroffbe4c4d12007-08-24 19:07:16 +00001181 Expr *&lex, Expr *&rex, SourceLocation loc, bool isCompAssign)
Steve Naroff1926c832007-04-24 00:23:05 +00001182{
Chris Lattner1d411a82007-06-05 20:52:21 +00001183 // FIXME: Shifts don't perform usual arithmetic conversions. This is wrong
1184 // for int << longlong -> the result type should be int, not long long.
Steve Naroffbe4c4d12007-08-24 19:07:16 +00001185 QualType compType = UsualArithmeticConversions(lex, rex, isCompAssign);
Steve Naroff1926c832007-04-24 00:23:05 +00001186
Steve Naroffdbd9e892007-07-17 00:58:39 +00001187 // handle the common case first (both operands are arithmetic).
1188 if (lex->getType()->isIntegerType() && rex->getType()->isIntegerType())
Steve Naroffbe4c4d12007-08-24 19:07:16 +00001189 return compType;
Steve Naroff6f49f5d2007-05-29 14:23:36 +00001190 InvalidOperands(loc, lex, rex);
Steve Naroff218bc2b2007-05-04 21:54:46 +00001191 return QualType();
Steve Naroff26c8ea52007-03-21 21:08:52 +00001192}
1193
Ted Kremenekfa907b82007-10-29 16:45:23 +00001194// Utility method to plow through parentheses to get the first nested
1195// non-ParenExpr expr.
1196static inline Expr* IgnoreParen(Expr* E) {
Ted Kremenek78036cd2007-10-30 21:03:09 +00001197 while (ParenExpr* P = dyn_cast<ParenExpr>(E))
1198 E = P->getSubExpr();
Ted Kremenekfa907b82007-10-29 16:45:23 +00001199
1200 return E;
1201}
1202
Chris Lattnerb620c342007-08-26 01:18:55 +00001203inline QualType Sema::CheckCompareOperands( // C99 6.5.8
1204 Expr *&lex, Expr *&rex, SourceLocation loc, bool isRelational)
Steve Naroff1926c832007-04-24 00:23:05 +00001205{
Chris Lattnerb620c342007-08-26 01:18:55 +00001206 // C99 6.5.8p3 / C99 6.5.9p4
Steve Naroff47fea352007-08-10 18:26:40 +00001207 if (lex->getType()->isArithmeticType() && rex->getType()->isArithmeticType())
1208 UsualArithmeticConversions(lex, rex);
1209 else {
1210 UsualUnaryConversions(lex);
1211 UsualUnaryConversions(rex);
1212 }
Steve Naroff31090012007-07-16 21:54:35 +00001213 QualType lType = lex->getType();
1214 QualType rType = rex->getType();
Steve Naroff1926c832007-04-24 00:23:05 +00001215
Ted Kremeneke2763b02007-10-29 17:13:39 +00001216
1217 // For non-floating point types, check for self-comparisons of the form
1218 // x == x, x != x, x < x, etc. These always evaluate to a constant, and
1219 // often indicate logic errors in the program.
Ted Kremeneke451eae2007-10-29 16:58:49 +00001220 if (!lType->isFloatingType()) {
1221 if (DeclRefExpr* DRL = dyn_cast<DeclRefExpr>(IgnoreParen(lex)))
1222 if (DeclRefExpr* DRR = dyn_cast<DeclRefExpr>(IgnoreParen(rex)))
1223 if (DRL->getDecl() == DRR->getDecl())
1224 Diag(loc, diag::warn_selfcomparison);
1225 }
1226
Chris Lattnerb620c342007-08-26 01:18:55 +00001227 if (isRelational) {
1228 if (lType->isRealType() && rType->isRealType())
1229 return Context.IntTy;
1230 } else {
Ted Kremeneke2763b02007-10-29 17:13:39 +00001231 // Check for comparisons of floating point operands using != and ==.
1232 // Issue a warning if these are no self-comparisons, as they are not likely
1233 // to do what the programmer intended.
1234 if (lType->isFloatingType()) {
1235 assert (rType->isFloatingType());
1236
Ted Kremenekd4ecc6d2007-10-29 16:40:01 +00001237 // Special case: check for x == x (which is OK).
1238 bool EmitWarning = true;
1239
Ted Kremenekfa907b82007-10-29 16:45:23 +00001240 if (DeclRefExpr* DRL = dyn_cast<DeclRefExpr>(IgnoreParen(lex)))
1241 if (DeclRefExpr* DRR = dyn_cast<DeclRefExpr>(IgnoreParen(rex)))
Ted Kremenekd4ecc6d2007-10-29 16:40:01 +00001242 if (DRL->getDecl() == DRR->getDecl())
1243 EmitWarning = false;
1244
1245 if (EmitWarning)
1246 Diag(loc, diag::warn_floatingpoint_eq);
1247 }
1248
Chris Lattnerb620c342007-08-26 01:18:55 +00001249 if (lType->isArithmeticType() && rType->isArithmeticType())
1250 return Context.IntTy;
1251 }
Steve Naroffe4718892007-04-27 18:30:00 +00001252
Chris Lattner1895e582007-08-26 01:10:14 +00001253 bool LHSIsNull = lex->isNullPointerConstant(Context);
1254 bool RHSIsNull = rex->isNullPointerConstant(Context);
1255
Chris Lattnerb620c342007-08-26 01:18:55 +00001256 // All of the following pointer related warnings are GCC extensions, except
1257 // when handling null pointer constants. One day, we can consider making them
1258 // errors (when -pedantic-errors is enabled).
Steve Naroff808eb8f2007-08-27 04:08:11 +00001259 if (lType->isPointerType() && rType->isPointerType()) { // C99 6.5.8p2
Chris Lattner1895e582007-08-26 01:10:14 +00001260 if (!LHSIsNull && !RHSIsNull &&
Steve Naroff32e44c02007-10-15 20:41:53 +00001261 !Context.pointerTypesAreCompatible(lType.getUnqualifiedType(),
1262 rType.getUnqualifiedType())) {
Steve Naroffcdee44c2007-08-16 21:48:38 +00001263 Diag(loc, diag::ext_typecheck_comparison_of_distinct_pointers,
1264 lType.getAsString(), rType.getAsString(),
1265 lex->getSourceRange(), rex->getSourceRange());
Steve Naroff75c17232007-06-13 21:41:08 +00001266 }
Chris Lattner1895e582007-08-26 01:10:14 +00001267 promoteExprToType(rex, lType); // promote the pointer to pointer
Steve Naroffcdee44c2007-08-16 21:48:38 +00001268 return Context.IntTy;
1269 }
1270 if (lType->isPointerType() && rType->isIntegerType()) {
Chris Lattner1895e582007-08-26 01:10:14 +00001271 if (!RHSIsNull)
Steve Naroffcdee44c2007-08-16 21:48:38 +00001272 Diag(loc, diag::ext_typecheck_comparison_of_pointer_integer,
1273 lType.getAsString(), rType.getAsString(),
1274 lex->getSourceRange(), rex->getSourceRange());
Chris Lattner1895e582007-08-26 01:10:14 +00001275 promoteExprToType(rex, lType); // promote the integer to pointer
Steve Naroffcdee44c2007-08-16 21:48:38 +00001276 return Context.IntTy;
1277 }
1278 if (lType->isIntegerType() && rType->isPointerType()) {
Chris Lattner1895e582007-08-26 01:10:14 +00001279 if (!LHSIsNull)
Steve Naroffcdee44c2007-08-16 21:48:38 +00001280 Diag(loc, diag::ext_typecheck_comparison_of_pointer_integer,
1281 lType.getAsString(), rType.getAsString(),
1282 lex->getSourceRange(), rex->getSourceRange());
Chris Lattner1895e582007-08-26 01:10:14 +00001283 promoteExprToType(lex, rType); // promote the integer to pointer
Steve Naroffcdee44c2007-08-16 21:48:38 +00001284 return Context.IntTy;
Steve Naroff043d45d2007-05-15 02:32:35 +00001285 }
Steve Naroff6f49f5d2007-05-29 14:23:36 +00001286 InvalidOperands(loc, lex, rex);
Steve Naroff218bc2b2007-05-04 21:54:46 +00001287 return QualType();
Steve Naroff26c8ea52007-03-21 21:08:52 +00001288}
1289
Steve Naroff218bc2b2007-05-04 21:54:46 +00001290inline QualType Sema::CheckBitwiseOperands(
Steve Naroffbe4c4d12007-08-24 19:07:16 +00001291 Expr *&lex, Expr *&rex, SourceLocation loc, bool isCompAssign)
Steve Naroff1926c832007-04-24 00:23:05 +00001292{
Steve Naroff94a5aca2007-07-16 22:23:01 +00001293 if (lex->getType()->isVectorType() || rex->getType()->isVectorType())
Steve Naroff84ff4b42007-07-09 21:31:10 +00001294 return CheckVectorOperands(loc, lex, rex);
Steve Naroffb8ea4fb2007-07-13 23:32:42 +00001295
Steve Naroffbe4c4d12007-08-24 19:07:16 +00001296 QualType compType = UsualArithmeticConversions(lex, rex, isCompAssign);
Steve Naroff1926c832007-04-24 00:23:05 +00001297
Steve Naroffdbd9e892007-07-17 00:58:39 +00001298 if (lex->getType()->isIntegerType() && rex->getType()->isIntegerType())
Steve Naroffbe4c4d12007-08-24 19:07:16 +00001299 return compType;
Steve Naroff6f49f5d2007-05-29 14:23:36 +00001300 InvalidOperands(loc, lex, rex);
Steve Naroff218bc2b2007-05-04 21:54:46 +00001301 return QualType();
Steve Naroff26c8ea52007-03-21 21:08:52 +00001302}
1303
Steve Naroff218bc2b2007-05-04 21:54:46 +00001304inline QualType Sema::CheckLogicalOperands( // C99 6.5.[13,14]
Steve Naroff7a5af782007-07-13 16:58:59 +00001305 Expr *&lex, Expr *&rex, SourceLocation loc)
Steve Naroff1926c832007-04-24 00:23:05 +00001306{
Steve Naroff31090012007-07-16 21:54:35 +00001307 UsualUnaryConversions(lex);
1308 UsualUnaryConversions(rex);
Steve Naroffe4718892007-04-27 18:30:00 +00001309
Steve Naroffdbd9e892007-07-17 00:58:39 +00001310 if (lex->getType()->isScalarType() || rex->getType()->isScalarType())
Steve Naroff218bc2b2007-05-04 21:54:46 +00001311 return Context.IntTy;
Steve Naroff6f49f5d2007-05-29 14:23:36 +00001312 InvalidOperands(loc, lex, rex);
Steve Naroff218bc2b2007-05-04 21:54:46 +00001313 return QualType();
Steve Naroffae4143e2007-04-26 20:39:23 +00001314}
1315
Steve Naroff35d85152007-05-07 00:24:15 +00001316inline QualType Sema::CheckAssignmentOperands( // C99 6.5.16.1
Steve Naroff0c1c7ed2007-08-24 22:33:52 +00001317 Expr *lex, Expr *&rex, SourceLocation loc, QualType compoundType)
Steve Naroffae4143e2007-04-26 20:39:23 +00001318{
Steve Naroff0af91202007-04-27 21:51:21 +00001319 QualType lhsType = lex->getType();
Steve Naroff35d85152007-05-07 00:24:15 +00001320 QualType rhsType = compoundType.isNull() ? rex->getType() : compoundType;
Steve Naroff1f4d7272007-05-11 04:00:31 +00001321 bool hadError = false;
Steve Naroff9358c712007-05-27 23:58:33 +00001322 Expr::isModifiableLvalueResult mlval = lex->isModifiableLvalue();
1323
1324 switch (mlval) { // C99 6.5.16p2
1325 case Expr::MLV_Valid:
1326 break;
1327 case Expr::MLV_ConstQualified:
1328 Diag(loc, diag::err_typecheck_assign_const, lex->getSourceRange());
1329 hadError = true;
1330 break;
1331 case Expr::MLV_ArrayType:
1332 Diag(loc, diag::err_typecheck_array_not_modifiable_lvalue,
1333 lhsType.getAsString(), lex->getSourceRange());
1334 return QualType();
1335 case Expr::MLV_NotObjectType:
1336 Diag(loc, diag::err_typecheck_non_object_not_modifiable_lvalue,
1337 lhsType.getAsString(), lex->getSourceRange());
1338 return QualType();
1339 case Expr::MLV_InvalidExpression:
1340 Diag(loc, diag::err_typecheck_expression_not_modifiable_lvalue,
1341 lex->getSourceRange());
1342 return QualType();
1343 case Expr::MLV_IncompleteType:
1344 case Expr::MLV_IncompleteVoidType:
1345 Diag(loc, diag::err_typecheck_incomplete_type_not_modifiable_lvalue,
1346 lhsType.getAsString(), lex->getSourceRange());
1347 return QualType();
Steve Naroff0d595ca2007-07-30 03:29:09 +00001348 case Expr::MLV_DuplicateVectorComponents:
1349 Diag(loc, diag::err_typecheck_duplicate_vector_components_not_mlvalue,
1350 lex->getSourceRange());
1351 return QualType();
Steve Naroff218bc2b2007-05-04 21:54:46 +00001352 }
Steve Naroffb8ea4fb2007-07-13 23:32:42 +00001353 AssignmentCheckResult result;
1354
1355 if (compoundType.isNull())
1356 result = CheckSingleAssignmentConstraints(lhsType, rex);
1357 else
1358 result = CheckCompoundAssignmentConstraints(lhsType, rhsType);
Steve Naroffad373bd2007-07-31 12:34:36 +00001359
Steve Naroff218bc2b2007-05-04 21:54:46 +00001360 // decode the result (notice that extensions still return a type).
1361 switch (result) {
1362 case Compatible:
Steve Naroff1f4d7272007-05-11 04:00:31 +00001363 break;
Steve Naroff218bc2b2007-05-04 21:54:46 +00001364 case Incompatible:
Steve Naroffe845e272007-05-18 01:06:45 +00001365 Diag(loc, diag::err_typecheck_assign_incompatible,
Chris Lattner84e160a2007-05-19 07:03:17 +00001366 lhsType.getAsString(), rhsType.getAsString(),
1367 lex->getSourceRange(), rex->getSourceRange());
Steve Naroff1f4d7272007-05-11 04:00:31 +00001368 hadError = true;
1369 break;
Steve Naroff218bc2b2007-05-04 21:54:46 +00001370 case PointerFromInt:
1371 // check for null pointer constant (C99 6.3.2.3p3)
Chris Lattner0e9d6222007-07-15 23:26:56 +00001372 if (compoundType.isNull() && !rex->isNullPointerConstant(Context)) {
Steve Naroff56faab22007-05-30 04:20:12 +00001373 Diag(loc, diag::ext_typecheck_assign_pointer_int,
1374 lhsType.getAsString(), rhsType.getAsString(),
Steve Naroff9358c712007-05-27 23:58:33 +00001375 lex->getSourceRange(), rex->getSourceRange());
Steve Naroff9992bba2007-05-30 16:27:15 +00001376 }
Steve Naroff1f4d7272007-05-11 04:00:31 +00001377 break;
Steve Naroff56faab22007-05-30 04:20:12 +00001378 case IntFromPointer:
1379 Diag(loc, diag::ext_typecheck_assign_pointer_int,
1380 lhsType.getAsString(), rhsType.getAsString(),
Steve Naroff9358c712007-05-27 23:58:33 +00001381 lex->getSourceRange(), rex->getSourceRange());
Steve Naroff1f4d7272007-05-11 04:00:31 +00001382 break;
Steve Naroff218bc2b2007-05-04 21:54:46 +00001383 case IncompatiblePointer:
Steve Naroff9358c712007-05-27 23:58:33 +00001384 Diag(loc, diag::ext_typecheck_assign_incompatible_pointer,
1385 lhsType.getAsString(), rhsType.getAsString(),
1386 lex->getSourceRange(), rex->getSourceRange());
Steve Naroff1f4d7272007-05-11 04:00:31 +00001387 break;
1388 case CompatiblePointerDiscardsQualifiers:
Steve Naroff9358c712007-05-27 23:58:33 +00001389 Diag(loc, diag::ext_typecheck_assign_discards_qualifiers,
1390 lhsType.getAsString(), rhsType.getAsString(),
1391 lex->getSourceRange(), rex->getSourceRange());
Steve Naroff1f4d7272007-05-11 04:00:31 +00001392 break;
Steve Naroff218bc2b2007-05-04 21:54:46 +00001393 }
Steve Naroff98cf3e92007-06-06 18:38:38 +00001394 // C99 6.5.16p3: The type of an assignment expression is the type of the
1395 // left operand unless the left operand has qualified type, in which case
1396 // it is the unqualified version of the type of the left operand.
1397 // C99 6.5.16.1p2: In simple assignment, the value of the right operand
1398 // is converted to the type of the assignment expression (above).
Chris Lattnerf17bd422007-08-30 17:45:32 +00001399 // C++ 5.17p1: the type of the assignment expression is that of its left
1400 // oprdu.
Steve Naroff98cf3e92007-06-06 18:38:38 +00001401 return hadError ? QualType() : lhsType.getUnqualifiedType();
Steve Naroffae4143e2007-04-26 20:39:23 +00001402}
1403
Steve Naroff218bc2b2007-05-04 21:54:46 +00001404inline QualType Sema::CheckCommaOperands( // C99 6.5.17
Steve Naroff7a5af782007-07-13 16:58:59 +00001405 Expr *&lex, Expr *&rex, SourceLocation loc) {
Steve Naroff31090012007-07-16 21:54:35 +00001406 UsualUnaryConversions(rex);
1407 return rex->getType();
Steve Naroff95af0132007-03-30 23:47:58 +00001408}
1409
Steve Naroff7a5af782007-07-13 16:58:59 +00001410/// CheckIncrementDecrementOperand - unlike most "Check" methods, this routine
1411/// doesn't need to call UsualUnaryConversions or UsualArithmeticConversions.
Steve Naroff71ce2e02007-05-18 22:53:50 +00001412QualType Sema::CheckIncrementDecrementOperand(Expr *op, SourceLocation OpLoc) {
Steve Naroff7a5af782007-07-13 16:58:59 +00001413 QualType resType = op->getType();
Steve Naroff35d85152007-05-07 00:24:15 +00001414 assert(!resType.isNull() && "no type for increment/decrement expression");
Steve Naroffd50c88e2007-04-05 21:15:20 +00001415
Steve Naroff9d139172007-08-24 17:20:07 +00001416 // C99 6.5.2.4p1: We allow complex as a GCC extension.
Steve Naroff5f9ae642007-11-11 14:15:57 +00001417 if (const PointerType *pt = resType->getAsPointerType()) {
Steve Naroff35d85152007-05-07 00:24:15 +00001418 if (!pt->getPointeeType()->isObjectType()) { // C99 6.5.2.4p2, 6.5.6p2
Steve Naroff71ce2e02007-05-18 22:53:50 +00001419 Diag(OpLoc, diag::err_typecheck_arithmetic_incomplete_type,
1420 resType.getAsString(), op->getSourceRange());
Steve Naroff35d85152007-05-07 00:24:15 +00001421 return QualType();
1422 }
Steve Naroff9d139172007-08-24 17:20:07 +00001423 } else if (!resType->isRealType()) {
1424 if (resType->isComplexType())
1425 // C99 does not support ++/-- on complex types.
1426 Diag(OpLoc, diag::ext_integer_increment_complex,
1427 resType.getAsString(), op->getSourceRange());
1428 else {
1429 Diag(OpLoc, diag::err_typecheck_illegal_increment_decrement,
1430 resType.getAsString(), op->getSourceRange());
1431 return QualType();
1432 }
Steve Naroff46ba1eb2007-04-03 23:13:13 +00001433 }
Steve Naroff9e1e5512007-08-23 21:37:33 +00001434 // At this point, we know we have a real, complex or pointer type.
1435 // Now make sure the operand is a modifiable lvalue.
Steve Naroff9358c712007-05-27 23:58:33 +00001436 Expr::isModifiableLvalueResult mlval = op->isModifiableLvalue();
1437 if (mlval != Expr::MLV_Valid) {
1438 // FIXME: emit a more precise diagnostic...
Steve Naroff71ce2e02007-05-18 22:53:50 +00001439 Diag(OpLoc, diag::err_typecheck_invalid_lvalue_incr_decr,
Chris Lattner84e160a2007-05-19 07:03:17 +00001440 op->getSourceRange());
Steve Naroff35d85152007-05-07 00:24:15 +00001441 return QualType();
1442 }
1443 return resType;
Steve Naroff26c8ea52007-03-21 21:08:52 +00001444}
1445
Steve Naroff1926c832007-04-24 00:23:05 +00001446/// getPrimaryDeclaration - Helper function for CheckAddressOfOperand().
Steve Naroff47500512007-04-19 23:00:49 +00001447/// This routine allows us to typecheck complex/recursive expressions
1448/// where the declaration is needed for type checking. Here are some
1449/// examples: &s.xx, &s.zz[1].yy, &(1+2), &(XX), &"123"[2].
Steve Naroff1926c832007-04-24 00:23:05 +00001450static Decl *getPrimaryDeclaration(Expr *e) {
Steve Naroff47500512007-04-19 23:00:49 +00001451 switch (e->getStmtClass()) {
1452 case Stmt::DeclRefExprClass:
1453 return cast<DeclRefExpr>(e)->getDecl();
1454 case Stmt::MemberExprClass:
Steve Naroff1926c832007-04-24 00:23:05 +00001455 return getPrimaryDeclaration(cast<MemberExpr>(e)->getBase());
Steve Naroff47500512007-04-19 23:00:49 +00001456 case Stmt::ArraySubscriptExprClass:
Steve Naroff1926c832007-04-24 00:23:05 +00001457 return getPrimaryDeclaration(cast<ArraySubscriptExpr>(e)->getBase());
Steve Naroff47500512007-04-19 23:00:49 +00001458 case Stmt::CallExprClass:
Steve Naroff1926c832007-04-24 00:23:05 +00001459 return getPrimaryDeclaration(cast<CallExpr>(e)->getCallee());
Steve Naroff47500512007-04-19 23:00:49 +00001460 case Stmt::UnaryOperatorClass:
Steve Naroff1926c832007-04-24 00:23:05 +00001461 return getPrimaryDeclaration(cast<UnaryOperator>(e)->getSubExpr());
Steve Naroff47500512007-04-19 23:00:49 +00001462 case Stmt::ParenExprClass:
Steve Naroff1926c832007-04-24 00:23:05 +00001463 return getPrimaryDeclaration(cast<ParenExpr>(e)->getSubExpr());
Steve Naroff47500512007-04-19 23:00:49 +00001464 default:
1465 return 0;
1466 }
1467}
1468
1469/// CheckAddressOfOperand - The operand of & must be either a function
1470/// designator or an lvalue designating an object. If it is an lvalue, the
1471/// object cannot be declared with storage class register or be a bit field.
1472/// Note: The usual conversions are *not* applied to the operand of the &
Steve Naroffa78fe7e2007-05-16 19:47:19 +00001473/// operator (C99 6.3.2.1p[2-4]), and its result is never an lvalue.
Steve Naroff35d85152007-05-07 00:24:15 +00001474QualType Sema::CheckAddressOfOperand(Expr *op, SourceLocation OpLoc) {
Steve Naroff1926c832007-04-24 00:23:05 +00001475 Decl *dcl = getPrimaryDeclaration(op);
Steve Naroff9358c712007-05-27 23:58:33 +00001476 Expr::isLvalueResult lval = op->isLvalue();
Steve Naroff47500512007-04-19 23:00:49 +00001477
Steve Naroff9358c712007-05-27 23:58:33 +00001478 if (lval != Expr::LV_Valid) { // C99 6.5.3.2p1
Steve Naroff5dd642e2007-05-14 18:14:51 +00001479 if (dcl && isa<FunctionDecl>(dcl)) // allow function designators
1480 ;
Steve Naroff9358c712007-05-27 23:58:33 +00001481 else { // FIXME: emit more specific diag...
Steve Naroff71ce2e02007-05-18 22:53:50 +00001482 Diag(OpLoc, diag::err_typecheck_invalid_lvalue_addrof,
1483 op->getSourceRange());
Steve Naroff35d85152007-05-07 00:24:15 +00001484 return QualType();
1485 }
Steve Naroff47500512007-04-19 23:00:49 +00001486 } else if (dcl) {
1487 // We have an lvalue with a decl. Make sure the decl is not declared
1488 // with the register storage-class specifier.
1489 if (const VarDecl *vd = dyn_cast<VarDecl>(dcl)) {
Steve Naroff35d85152007-05-07 00:24:15 +00001490 if (vd->getStorageClass() == VarDecl::Register) {
Steve Naroff71ce2e02007-05-18 22:53:50 +00001491 Diag(OpLoc, diag::err_typecheck_address_of_register,
Chris Lattner84e160a2007-05-19 07:03:17 +00001492 op->getSourceRange());
Steve Naroff35d85152007-05-07 00:24:15 +00001493 return QualType();
1494 }
Steve Narofff633d092007-04-25 19:01:39 +00001495 } else
1496 assert(0 && "Unknown/unexpected decl type");
1497
Steve Naroff47500512007-04-19 23:00:49 +00001498 // FIXME: add check for bitfields!
1499 }
1500 // If the operand has type "type", the result has type "pointer to type".
Steve Naroff35d85152007-05-07 00:24:15 +00001501 return Context.getPointerType(op->getType());
Steve Naroff47500512007-04-19 23:00:49 +00001502}
1503
Steve Naroff35d85152007-05-07 00:24:15 +00001504QualType Sema::CheckIndirectionOperand(Expr *op, SourceLocation OpLoc) {
Steve Naroff31090012007-07-16 21:54:35 +00001505 UsualUnaryConversions(op);
1506 QualType qType = op->getType();
Steve Naroff35d85152007-05-07 00:24:15 +00001507
Chris Lattnerc996b172007-07-31 16:53:04 +00001508 if (const PointerType *PT = qType->getAsPointerType()) {
Steve Naroff758ada12007-05-28 16:15:57 +00001509 QualType ptype = PT->getPointeeType();
1510 // C99 6.5.3.2p4. "if it points to an object,...".
1511 if (ptype->isIncompleteType()) { // An incomplete type is not an object
1512 // GCC compat: special case 'void *' (treat as warning).
1513 if (ptype->isVoidType()) {
1514 Diag(OpLoc, diag::ext_typecheck_deref_ptr_to_void,
Steve Naroffeb9da942007-05-30 00:06:37 +00001515 qType.getAsString(), op->getSourceRange());
Steve Naroff758ada12007-05-28 16:15:57 +00001516 } else {
1517 Diag(OpLoc, diag::err_typecheck_deref_incomplete_type,
Steve Naroffeb9da942007-05-30 00:06:37 +00001518 ptype.getAsString(), op->getSourceRange());
Steve Naroff758ada12007-05-28 16:15:57 +00001519 return QualType();
1520 }
1521 }
1522 return ptype;
1523 }
1524 Diag(OpLoc, diag::err_typecheck_indirection_requires_pointer,
Steve Naroffeb9da942007-05-30 00:06:37 +00001525 qType.getAsString(), op->getSourceRange());
Steve Naroff35d85152007-05-07 00:24:15 +00001526 return QualType();
Steve Naroff1926c832007-04-24 00:23:05 +00001527}
Steve Naroff218bc2b2007-05-04 21:54:46 +00001528
1529static inline BinaryOperator::Opcode ConvertTokenKindToBinaryOpcode(
1530 tok::TokenKind Kind) {
1531 BinaryOperator::Opcode Opc;
1532 switch (Kind) {
1533 default: assert(0 && "Unknown binop!");
1534 case tok::star: Opc = BinaryOperator::Mul; break;
1535 case tok::slash: Opc = BinaryOperator::Div; break;
1536 case tok::percent: Opc = BinaryOperator::Rem; break;
1537 case tok::plus: Opc = BinaryOperator::Add; break;
1538 case tok::minus: Opc = BinaryOperator::Sub; break;
1539 case tok::lessless: Opc = BinaryOperator::Shl; break;
1540 case tok::greatergreater: Opc = BinaryOperator::Shr; break;
1541 case tok::lessequal: Opc = BinaryOperator::LE; break;
1542 case tok::less: Opc = BinaryOperator::LT; break;
1543 case tok::greaterequal: Opc = BinaryOperator::GE; break;
1544 case tok::greater: Opc = BinaryOperator::GT; break;
1545 case tok::exclaimequal: Opc = BinaryOperator::NE; break;
1546 case tok::equalequal: Opc = BinaryOperator::EQ; break;
1547 case tok::amp: Opc = BinaryOperator::And; break;
1548 case tok::caret: Opc = BinaryOperator::Xor; break;
1549 case tok::pipe: Opc = BinaryOperator::Or; break;
1550 case tok::ampamp: Opc = BinaryOperator::LAnd; break;
1551 case tok::pipepipe: Opc = BinaryOperator::LOr; break;
1552 case tok::equal: Opc = BinaryOperator::Assign; break;
1553 case tok::starequal: Opc = BinaryOperator::MulAssign; break;
1554 case tok::slashequal: Opc = BinaryOperator::DivAssign; break;
1555 case tok::percentequal: Opc = BinaryOperator::RemAssign; break;
1556 case tok::plusequal: Opc = BinaryOperator::AddAssign; break;
1557 case tok::minusequal: Opc = BinaryOperator::SubAssign; break;
1558 case tok::lesslessequal: Opc = BinaryOperator::ShlAssign; break;
1559 case tok::greatergreaterequal: Opc = BinaryOperator::ShrAssign; break;
1560 case tok::ampequal: Opc = BinaryOperator::AndAssign; break;
1561 case tok::caretequal: Opc = BinaryOperator::XorAssign; break;
1562 case tok::pipeequal: Opc = BinaryOperator::OrAssign; break;
1563 case tok::comma: Opc = BinaryOperator::Comma; break;
1564 }
1565 return Opc;
1566}
1567
Steve Naroff35d85152007-05-07 00:24:15 +00001568static inline UnaryOperator::Opcode ConvertTokenKindToUnaryOpcode(
1569 tok::TokenKind Kind) {
1570 UnaryOperator::Opcode Opc;
1571 switch (Kind) {
1572 default: assert(0 && "Unknown unary op!");
1573 case tok::plusplus: Opc = UnaryOperator::PreInc; break;
1574 case tok::minusminus: Opc = UnaryOperator::PreDec; break;
1575 case tok::amp: Opc = UnaryOperator::AddrOf; break;
1576 case tok::star: Opc = UnaryOperator::Deref; break;
1577 case tok::plus: Opc = UnaryOperator::Plus; break;
1578 case tok::minus: Opc = UnaryOperator::Minus; break;
1579 case tok::tilde: Opc = UnaryOperator::Not; break;
1580 case tok::exclaim: Opc = UnaryOperator::LNot; break;
1581 case tok::kw_sizeof: Opc = UnaryOperator::SizeOf; break;
1582 case tok::kw___alignof: Opc = UnaryOperator::AlignOf; break;
1583 case tok::kw___real: Opc = UnaryOperator::Real; break;
1584 case tok::kw___imag: Opc = UnaryOperator::Imag; break;
Chris Lattnerd0f76512007-06-08 22:16:53 +00001585 case tok::kw___extension__: Opc = UnaryOperator::Extension; break;
Steve Naroff35d85152007-05-07 00:24:15 +00001586 }
1587 return Opc;
1588}
1589
Steve Naroff218bc2b2007-05-04 21:54:46 +00001590// Binary Operators. 'Tok' is the token for the operator.
Steve Naroff83895f72007-09-16 03:34:24 +00001591Action::ExprResult Sema::ActOnBinOp(SourceLocation TokLoc, tok::TokenKind Kind,
Steve Naroff218bc2b2007-05-04 21:54:46 +00001592 ExprTy *LHS, ExprTy *RHS) {
1593 BinaryOperator::Opcode Opc = ConvertTokenKindToBinaryOpcode(Kind);
1594 Expr *lhs = (Expr *)LHS, *rhs = (Expr*)RHS;
1595
Steve Naroff83895f72007-09-16 03:34:24 +00001596 assert((lhs != 0) && "ActOnBinOp(): missing left expression");
1597 assert((rhs != 0) && "ActOnBinOp(): missing right expression");
Steve Naroff218bc2b2007-05-04 21:54:46 +00001598
Chris Lattner256c21b2007-06-28 03:53:10 +00001599 QualType ResultTy; // Result type of the binary operator.
1600 QualType CompTy; // Computation type for compound assignments (e.g. '+=')
Steve Naroff218bc2b2007-05-04 21:54:46 +00001601
1602 switch (Opc) {
1603 default:
1604 assert(0 && "Unknown binary expr!");
1605 case BinaryOperator::Assign:
Chris Lattner256c21b2007-06-28 03:53:10 +00001606 ResultTy = CheckAssignmentOperands(lhs, rhs, TokLoc, QualType());
Steve Naroff218bc2b2007-05-04 21:54:46 +00001607 break;
1608 case BinaryOperator::Mul:
1609 case BinaryOperator::Div:
Chris Lattner256c21b2007-06-28 03:53:10 +00001610 ResultTy = CheckMultiplyDivideOperands(lhs, rhs, TokLoc);
Steve Naroff218bc2b2007-05-04 21:54:46 +00001611 break;
1612 case BinaryOperator::Rem:
Chris Lattner256c21b2007-06-28 03:53:10 +00001613 ResultTy = CheckRemainderOperands(lhs, rhs, TokLoc);
Steve Naroff218bc2b2007-05-04 21:54:46 +00001614 break;
1615 case BinaryOperator::Add:
Chris Lattner256c21b2007-06-28 03:53:10 +00001616 ResultTy = CheckAdditionOperands(lhs, rhs, TokLoc);
Steve Naroff218bc2b2007-05-04 21:54:46 +00001617 break;
1618 case BinaryOperator::Sub:
Chris Lattner256c21b2007-06-28 03:53:10 +00001619 ResultTy = CheckSubtractionOperands(lhs, rhs, TokLoc);
Steve Naroff218bc2b2007-05-04 21:54:46 +00001620 break;
1621 case BinaryOperator::Shl:
1622 case BinaryOperator::Shr:
Chris Lattner256c21b2007-06-28 03:53:10 +00001623 ResultTy = CheckShiftOperands(lhs, rhs, TokLoc);
Steve Naroff218bc2b2007-05-04 21:54:46 +00001624 break;
1625 case BinaryOperator::LE:
1626 case BinaryOperator::LT:
1627 case BinaryOperator::GE:
1628 case BinaryOperator::GT:
Chris Lattnerb620c342007-08-26 01:18:55 +00001629 ResultTy = CheckCompareOperands(lhs, rhs, TokLoc, true);
Steve Naroff218bc2b2007-05-04 21:54:46 +00001630 break;
1631 case BinaryOperator::EQ:
1632 case BinaryOperator::NE:
Chris Lattnerb620c342007-08-26 01:18:55 +00001633 ResultTy = CheckCompareOperands(lhs, rhs, TokLoc, false);
Steve Naroff218bc2b2007-05-04 21:54:46 +00001634 break;
1635 case BinaryOperator::And:
1636 case BinaryOperator::Xor:
1637 case BinaryOperator::Or:
Chris Lattner256c21b2007-06-28 03:53:10 +00001638 ResultTy = CheckBitwiseOperands(lhs, rhs, TokLoc);
Steve Naroff218bc2b2007-05-04 21:54:46 +00001639 break;
1640 case BinaryOperator::LAnd:
1641 case BinaryOperator::LOr:
Chris Lattner256c21b2007-06-28 03:53:10 +00001642 ResultTy = CheckLogicalOperands(lhs, rhs, TokLoc);
Steve Naroff218bc2b2007-05-04 21:54:46 +00001643 break;
1644 case BinaryOperator::MulAssign:
1645 case BinaryOperator::DivAssign:
Steve Naroffbe4c4d12007-08-24 19:07:16 +00001646 CompTy = CheckMultiplyDivideOperands(lhs, rhs, TokLoc, true);
Chris Lattner256c21b2007-06-28 03:53:10 +00001647 if (!CompTy.isNull())
1648 ResultTy = CheckAssignmentOperands(lhs, rhs, TokLoc, CompTy);
Steve Naroff218bc2b2007-05-04 21:54:46 +00001649 break;
1650 case BinaryOperator::RemAssign:
Steve Naroffbe4c4d12007-08-24 19:07:16 +00001651 CompTy = CheckRemainderOperands(lhs, rhs, TokLoc, true);
Chris Lattner256c21b2007-06-28 03:53:10 +00001652 if (!CompTy.isNull())
1653 ResultTy = CheckAssignmentOperands(lhs, rhs, TokLoc, CompTy);
Steve Naroff218bc2b2007-05-04 21:54:46 +00001654 break;
1655 case BinaryOperator::AddAssign:
Steve Naroffbe4c4d12007-08-24 19:07:16 +00001656 CompTy = CheckAdditionOperands(lhs, rhs, TokLoc, true);
Chris Lattner256c21b2007-06-28 03:53:10 +00001657 if (!CompTy.isNull())
1658 ResultTy = CheckAssignmentOperands(lhs, rhs, TokLoc, CompTy);
Steve Naroff218bc2b2007-05-04 21:54:46 +00001659 break;
1660 case BinaryOperator::SubAssign:
Steve Naroffbe4c4d12007-08-24 19:07:16 +00001661 CompTy = CheckSubtractionOperands(lhs, rhs, TokLoc, true);
Chris Lattner256c21b2007-06-28 03:53:10 +00001662 if (!CompTy.isNull())
1663 ResultTy = CheckAssignmentOperands(lhs, rhs, TokLoc, CompTy);
Steve Naroff218bc2b2007-05-04 21:54:46 +00001664 break;
1665 case BinaryOperator::ShlAssign:
1666 case BinaryOperator::ShrAssign:
Steve Naroffbe4c4d12007-08-24 19:07:16 +00001667 CompTy = CheckShiftOperands(lhs, rhs, TokLoc, true);
Chris Lattner256c21b2007-06-28 03:53:10 +00001668 if (!CompTy.isNull())
1669 ResultTy = CheckAssignmentOperands(lhs, rhs, TokLoc, CompTy);
Steve Naroff218bc2b2007-05-04 21:54:46 +00001670 break;
1671 case BinaryOperator::AndAssign:
1672 case BinaryOperator::XorAssign:
1673 case BinaryOperator::OrAssign:
Steve Naroffbe4c4d12007-08-24 19:07:16 +00001674 CompTy = CheckBitwiseOperands(lhs, rhs, TokLoc, true);
Chris Lattner256c21b2007-06-28 03:53:10 +00001675 if (!CompTy.isNull())
1676 ResultTy = CheckAssignmentOperands(lhs, rhs, TokLoc, CompTy);
Steve Naroff218bc2b2007-05-04 21:54:46 +00001677 break;
1678 case BinaryOperator::Comma:
Chris Lattner256c21b2007-06-28 03:53:10 +00001679 ResultTy = CheckCommaOperands(lhs, rhs, TokLoc);
Steve Naroff218bc2b2007-05-04 21:54:46 +00001680 break;
1681 }
Chris Lattner256c21b2007-06-28 03:53:10 +00001682 if (ResultTy.isNull())
Steve Naroff218bc2b2007-05-04 21:54:46 +00001683 return true;
Chris Lattner256c21b2007-06-28 03:53:10 +00001684 if (CompTy.isNull())
Chris Lattnerc11005f2007-08-28 18:36:55 +00001685 return new BinaryOperator(lhs, rhs, Opc, ResultTy, TokLoc);
Chris Lattner256c21b2007-06-28 03:53:10 +00001686 else
Chris Lattnerc11005f2007-08-28 18:36:55 +00001687 return new CompoundAssignOperator(lhs, rhs, Opc, ResultTy, CompTy, TokLoc);
Steve Naroff218bc2b2007-05-04 21:54:46 +00001688}
1689
Steve Naroff35d85152007-05-07 00:24:15 +00001690// Unary Operators. 'Tok' is the token for the operator.
Steve Naroff83895f72007-09-16 03:34:24 +00001691Action::ExprResult Sema::ActOnUnaryOp(SourceLocation OpLoc, tok::TokenKind Op,
Chris Lattner86554282007-06-08 22:32:33 +00001692 ExprTy *input) {
1693 Expr *Input = (Expr*)input;
Steve Naroff35d85152007-05-07 00:24:15 +00001694 UnaryOperator::Opcode Opc = ConvertTokenKindToUnaryOpcode(Op);
1695 QualType resultType;
1696 switch (Opc) {
1697 default:
1698 assert(0 && "Unimplemented unary expr!");
1699 case UnaryOperator::PreInc:
1700 case UnaryOperator::PreDec:
Chris Lattner86554282007-06-08 22:32:33 +00001701 resultType = CheckIncrementDecrementOperand(Input, OpLoc);
Steve Naroff35d85152007-05-07 00:24:15 +00001702 break;
1703 case UnaryOperator::AddrOf:
Chris Lattner86554282007-06-08 22:32:33 +00001704 resultType = CheckAddressOfOperand(Input, OpLoc);
Steve Naroff35d85152007-05-07 00:24:15 +00001705 break;
1706 case UnaryOperator::Deref:
Chris Lattner86554282007-06-08 22:32:33 +00001707 resultType = CheckIndirectionOperand(Input, OpLoc);
Steve Naroff35d85152007-05-07 00:24:15 +00001708 break;
1709 case UnaryOperator::Plus:
1710 case UnaryOperator::Minus:
Steve Naroff31090012007-07-16 21:54:35 +00001711 UsualUnaryConversions(Input);
1712 resultType = Input->getType();
Steve Naroff35d85152007-05-07 00:24:15 +00001713 if (!resultType->isArithmeticType()) // C99 6.5.3.3p1
Chris Lattnerc04bd6a2007-05-16 18:09:54 +00001714 return Diag(OpLoc, diag::err_typecheck_unary_expr,
1715 resultType.getAsString());
Steve Naroff35d85152007-05-07 00:24:15 +00001716 break;
1717 case UnaryOperator::Not: // bitwise complement
Steve Naroff31090012007-07-16 21:54:35 +00001718 UsualUnaryConversions(Input);
1719 resultType = Input->getType();
Steve Naroff9d139172007-08-24 17:20:07 +00001720 // C99 6.5.3.3p1. We allow complex as a GCC extension.
1721 if (!resultType->isIntegerType()) {
1722 if (resultType->isComplexType())
1723 // C99 does not support '~' for complex conjugation.
1724 Diag(OpLoc, diag::ext_integer_complement_complex,
1725 resultType.getAsString());
1726 else
1727 return Diag(OpLoc, diag::err_typecheck_unary_expr,
1728 resultType.getAsString());
1729 }
Steve Naroff35d85152007-05-07 00:24:15 +00001730 break;
1731 case UnaryOperator::LNot: // logical negation
Steve Naroff71b59a92007-06-04 22:22:31 +00001732 // Unlike +/-/~, integer promotions aren't done here (C99 6.5.3.3p5).
Steve Naroff31090012007-07-16 21:54:35 +00001733 DefaultFunctionArrayConversion(Input);
1734 resultType = Input->getType();
Steve Naroff35d85152007-05-07 00:24:15 +00001735 if (!resultType->isScalarType()) // C99 6.5.3.3p1
Chris Lattnerc04bd6a2007-05-16 18:09:54 +00001736 return Diag(OpLoc, diag::err_typecheck_unary_expr,
1737 resultType.getAsString());
Chris Lattnerbe31ed82007-06-02 19:11:33 +00001738 // LNot always has type int. C99 6.5.3.3p5.
1739 resultType = Context.IntTy;
Steve Naroff35d85152007-05-07 00:24:15 +00001740 break;
1741 case UnaryOperator::SizeOf:
Chris Lattner86554282007-06-08 22:32:33 +00001742 resultType = CheckSizeOfAlignOfOperand(Input->getType(), OpLoc, true);
Steve Naroff043d45d2007-05-15 02:32:35 +00001743 break;
Steve Naroff35d85152007-05-07 00:24:15 +00001744 case UnaryOperator::AlignOf:
Chris Lattner86554282007-06-08 22:32:33 +00001745 resultType = CheckSizeOfAlignOfOperand(Input->getType(), OpLoc, false);
1746 break;
Chris Lattner30b5dd02007-08-24 21:16:53 +00001747 case UnaryOperator::Real:
Chris Lattner30b5dd02007-08-24 21:16:53 +00001748 case UnaryOperator::Imag:
Chris Lattner74ed76b2007-08-24 21:41:10 +00001749 resultType = CheckRealImagOperand(Input, OpLoc);
Chris Lattner30b5dd02007-08-24 21:16:53 +00001750 break;
Chris Lattner86554282007-06-08 22:32:33 +00001751 case UnaryOperator::Extension:
Chris Lattner86554282007-06-08 22:32:33 +00001752 resultType = Input->getType();
Steve Naroff043d45d2007-05-15 02:32:35 +00001753 break;
Steve Naroff35d85152007-05-07 00:24:15 +00001754 }
1755 if (resultType.isNull())
1756 return true;
Chris Lattner86554282007-06-08 22:32:33 +00001757 return new UnaryOperator(Input, Opc, resultType, OpLoc);
Steve Naroff35d85152007-05-07 00:24:15 +00001758}
Chris Lattnereefa10e2007-05-28 06:56:27 +00001759
Steve Naroff66356bd2007-09-16 14:56:35 +00001760/// ActOnAddrLabel - Parse the GNU address of label extension: "&&foo".
1761Sema::ExprResult Sema::ActOnAddrLabel(SourceLocation OpLoc,
Chris Lattnereefa10e2007-05-28 06:56:27 +00001762 SourceLocation LabLoc,
1763 IdentifierInfo *LabelII) {
1764 // Look up the record for this label identifier.
1765 LabelStmt *&LabelDecl = LabelMap[LabelII];
1766
1767 // If we haven't seen this label yet, create a forward reference.
1768 if (LabelDecl == 0)
1769 LabelDecl = new LabelStmt(LabLoc, LabelII, 0);
1770
1771 // Create the AST node. The address of a label always has type 'void*'.
Chris Lattnerd268a7a2007-08-03 17:31:20 +00001772 return new AddrLabelExpr(OpLoc, LabLoc, LabelDecl,
1773 Context.getPointerType(Context.VoidTy));
Chris Lattnereefa10e2007-05-28 06:56:27 +00001774}
1775
Steve Naroff66356bd2007-09-16 14:56:35 +00001776Sema::ExprResult Sema::ActOnStmtExpr(SourceLocation LPLoc, StmtTy *substmt,
Chris Lattner366727f2007-07-24 16:58:17 +00001777 SourceLocation RPLoc) { // "({..})"
1778 Stmt *SubStmt = static_cast<Stmt*>(substmt);
1779 assert(SubStmt && isa<CompoundStmt>(SubStmt) && "Invalid action invocation!");
1780 CompoundStmt *Compound = cast<CompoundStmt>(SubStmt);
1781
1782 // FIXME: there are a variety of strange constraints to enforce here, for
1783 // example, it is not possible to goto into a stmt expression apparently.
1784 // More semantic analysis is needed.
1785
1786 // FIXME: the last statement in the compount stmt has its value used. We
1787 // should not warn about it being unused.
1788
1789 // If there are sub stmts in the compound stmt, take the type of the last one
1790 // as the type of the stmtexpr.
1791 QualType Ty = Context.VoidTy;
1792
1793 if (!Compound->body_empty())
1794 if (Expr *LastExpr = dyn_cast<Expr>(Compound->body_back()))
1795 Ty = LastExpr->getType();
1796
1797 return new StmtExpr(Compound, Ty, LPLoc, RPLoc);
1798}
Steve Naroff78864672007-08-01 22:05:33 +00001799
Steve Naroff66356bd2007-09-16 14:56:35 +00001800Sema::ExprResult Sema::ActOnBuiltinOffsetOf(SourceLocation BuiltinLoc,
Chris Lattnerf17bd422007-08-30 17:45:32 +00001801 SourceLocation TypeLoc,
1802 TypeTy *argty,
1803 OffsetOfComponent *CompPtr,
1804 unsigned NumComponents,
1805 SourceLocation RPLoc) {
1806 QualType ArgTy = QualType::getFromOpaquePtr(argty);
1807 assert(!ArgTy.isNull() && "Missing type argument!");
1808
1809 // We must have at least one component that refers to the type, and the first
1810 // one is known to be a field designator. Verify that the ArgTy represents
1811 // a struct/union/class.
1812 if (!ArgTy->isRecordType())
1813 return Diag(TypeLoc, diag::err_offsetof_record_type,ArgTy.getAsString());
1814
1815 // Otherwise, create a compound literal expression as the base, and
1816 // iteratively process the offsetof designators.
1817 Expr *Res = new CompoundLiteralExpr(ArgTy, 0);
1818
Chris Lattner78502cf2007-08-31 21:49:13 +00001819 // offsetof with non-identifier designators (e.g. "offsetof(x, a.b[c])") are a
1820 // GCC extension, diagnose them.
1821 if (NumComponents != 1)
1822 Diag(BuiltinLoc, diag::ext_offsetof_extended_field_designator,
1823 SourceRange(CompPtr[1].LocStart, CompPtr[NumComponents-1].LocEnd));
1824
Chris Lattnerf17bd422007-08-30 17:45:32 +00001825 for (unsigned i = 0; i != NumComponents; ++i) {
1826 const OffsetOfComponent &OC = CompPtr[i];
1827 if (OC.isBrackets) {
1828 // Offset of an array sub-field. TODO: Should we allow vector elements?
1829 const ArrayType *AT = Res->getType()->getAsArrayType();
1830 if (!AT) {
1831 delete Res;
1832 return Diag(OC.LocEnd, diag::err_offsetof_array_type,
1833 Res->getType().getAsString());
1834 }
1835
Chris Lattner98dbf0a2007-08-30 17:59:59 +00001836 // FIXME: C++: Verify that operator[] isn't overloaded.
1837
Chris Lattnerf17bd422007-08-30 17:45:32 +00001838 // C99 6.5.2.1p1
1839 Expr *Idx = static_cast<Expr*>(OC.U.E);
1840 if (!Idx->getType()->isIntegerType())
1841 return Diag(Idx->getLocStart(), diag::err_typecheck_subscript,
1842 Idx->getSourceRange());
1843
1844 Res = new ArraySubscriptExpr(Res, Idx, AT->getElementType(), OC.LocEnd);
1845 continue;
1846 }
1847
1848 const RecordType *RC = Res->getType()->getAsRecordType();
1849 if (!RC) {
1850 delete Res;
1851 return Diag(OC.LocEnd, diag::err_offsetof_record_type,
1852 Res->getType().getAsString());
1853 }
1854
1855 // Get the decl corresponding to this.
1856 RecordDecl *RD = RC->getDecl();
1857 FieldDecl *MemberDecl = RD->getMember(OC.U.IdentInfo);
1858 if (!MemberDecl)
1859 return Diag(BuiltinLoc, diag::err_typecheck_no_member,
1860 OC.U.IdentInfo->getName(),
1861 SourceRange(OC.LocStart, OC.LocEnd));
Chris Lattner98dbf0a2007-08-30 17:59:59 +00001862
1863 // FIXME: C++: Verify that MemberDecl isn't a static field.
1864 // FIXME: Verify that MemberDecl isn't a bitfield.
1865
Chris Lattnerf17bd422007-08-30 17:45:32 +00001866 Res = new MemberExpr(Res, false, MemberDecl, OC.LocEnd);
1867 }
1868
1869 return new UnaryOperator(Res, UnaryOperator::OffsetOf, Context.getSizeType(),
1870 BuiltinLoc);
1871}
1872
1873
Steve Naroff66356bd2007-09-16 14:56:35 +00001874Sema::ExprResult Sema::ActOnTypesCompatibleExpr(SourceLocation BuiltinLoc,
Steve Naroff78864672007-08-01 22:05:33 +00001875 TypeTy *arg1, TypeTy *arg2,
1876 SourceLocation RPLoc) {
1877 QualType argT1 = QualType::getFromOpaquePtr(arg1);
1878 QualType argT2 = QualType::getFromOpaquePtr(arg2);
1879
1880 assert((!argT1.isNull() && !argT2.isNull()) && "Missing type argument(s)");
1881
Chris Lattnerf17bd422007-08-30 17:45:32 +00001882 return new TypesCompatibleExpr(Context.IntTy, BuiltinLoc, argT1, argT2,RPLoc);
Steve Naroff78864672007-08-01 22:05:33 +00001883}
1884
Steve Naroff66356bd2007-09-16 14:56:35 +00001885Sema::ExprResult Sema::ActOnChooseExpr(SourceLocation BuiltinLoc, ExprTy *cond,
Steve Naroff9efdabc2007-08-03 21:21:27 +00001886 ExprTy *expr1, ExprTy *expr2,
1887 SourceLocation RPLoc) {
1888 Expr *CondExpr = static_cast<Expr*>(cond);
1889 Expr *LHSExpr = static_cast<Expr*>(expr1);
1890 Expr *RHSExpr = static_cast<Expr*>(expr2);
1891
1892 assert((CondExpr && LHSExpr && RHSExpr) && "Missing type argument(s)");
1893
1894 // The conditional expression is required to be a constant expression.
1895 llvm::APSInt condEval(32);
1896 SourceLocation ExpLoc;
1897 if (!CondExpr->isIntegerConstantExpr(condEval, Context, &ExpLoc))
1898 return Diag(ExpLoc, diag::err_typecheck_choose_expr_requires_constant,
1899 CondExpr->getSourceRange());
1900
1901 // If the condition is > zero, then the AST type is the same as the LSHExpr.
1902 QualType resType = condEval.getZExtValue() ? LHSExpr->getType() :
1903 RHSExpr->getType();
1904 return new ChooseExpr(BuiltinLoc, CondExpr, LHSExpr, RHSExpr, resType, RPLoc);
1905}
1906
Anders Carlsson7e13ab82007-10-15 20:28:48 +00001907Sema::ExprResult Sema::ActOnVAArg(SourceLocation BuiltinLoc,
1908 ExprTy *expr, TypeTy *type,
1909 SourceLocation RPLoc)
1910{
1911 Expr *E = static_cast<Expr*>(expr);
1912 QualType T = QualType::getFromOpaquePtr(type);
1913
1914 InitBuiltinVaListType();
1915
1916 Sema::AssignmentCheckResult result;
1917
1918 result = CheckAssignmentConstraints(Context.getBuiltinVaListType(),
1919 E->getType());
1920 if (result != Compatible)
1921 return Diag(E->getLocStart(),
1922 diag::err_first_argument_to_va_arg_not_of_type_va_list,
1923 E->getType().getAsString(),
1924 E->getSourceRange());
1925
1926 // FIXME: Warn if a non-POD type is passed in.
1927
1928 return new VAArgExpr(BuiltinLoc, E, T, RPLoc);
1929}
1930
Anders Carlsson76f4a902007-08-21 17:43:55 +00001931// TODO: Move this to SemaObjC.cpp
Steve Naroffa397efd2007-11-03 11:27:19 +00001932Sema::ExprResult Sema::ParseObjCStringLiteral(SourceLocation AtLoc,
1933 ExprTy *string) {
Anders Carlsson76f4a902007-08-21 17:43:55 +00001934 StringLiteral* S = static_cast<StringLiteral *>(string);
1935
1936 if (CheckBuiltinCFStringArgument(S))
1937 return true;
1938
Steve Narofff73b7842007-10-15 23:35:17 +00001939 if (Context.getObjcConstantStringInterface().isNull()) {
1940 // Initialize the constant string interface lazily. This assumes
1941 // the NSConstantString interface is seen in this translation unit.
1942 IdentifierInfo *NSIdent = &Context.Idents.get("NSConstantString");
1943 ScopedDecl *IFace = LookupScopedDecl(NSIdent, Decl::IDNS_Ordinary,
1944 SourceLocation(), TUScope);
Steve Naroff69849552007-10-16 00:00:18 +00001945 ObjcInterfaceDecl *strIFace = dyn_cast_or_null<ObjcInterfaceDecl>(IFace);
Steve Naroff684dc412007-10-18 23:53:51 +00001946 if (!strIFace)
1947 return Diag(S->getLocStart(), diag::err_undef_interface,
1948 NSIdent->getName());
Steve Naroff69849552007-10-16 00:00:18 +00001949 Context.setObjcConstantStringInterface(strIFace);
Steve Narofff73b7842007-10-15 23:35:17 +00001950 }
1951 QualType t = Context.getObjcConstantStringInterface();
Anders Carlsson76f4a902007-08-21 17:43:55 +00001952 t = Context.getPointerType(t);
Steve Naroffa397efd2007-11-03 11:27:19 +00001953 return new ObjCStringLiteral(S, t, AtLoc);
Anders Carlsson76f4a902007-08-21 17:43:55 +00001954}
Anders Carlssonc5a81eb2007-08-22 15:14:15 +00001955
1956Sema::ExprResult Sema::ParseObjCEncodeExpression(SourceLocation AtLoc,
Chris Lattner37390be2007-10-16 22:51:17 +00001957 SourceLocation EncodeLoc,
Anders Carlssonc5a81eb2007-08-22 15:14:15 +00001958 SourceLocation LParenLoc,
1959 TypeTy *Ty,
1960 SourceLocation RParenLoc) {
1961 QualType EncodedType = QualType::getFromOpaquePtr(Ty);
1962
1963 QualType t = Context.getPointerType(Context.CharTy);
1964 return new ObjCEncodeExpr(t, EncodedType, AtLoc, RParenLoc);
1965}
Steve Narofff0f2afc2007-09-17 21:01:15 +00001966
Fariborz Jahanian4bef4622007-10-16 20:40:23 +00001967Sema::ExprResult Sema::ParseObjCSelectorExpression(Selector Sel,
1968 SourceLocation AtLoc,
Fariborz Jahanian6bd1d612007-10-16 23:21:02 +00001969 SourceLocation SelLoc,
Fariborz Jahanian4bef4622007-10-16 20:40:23 +00001970 SourceLocation LParenLoc,
1971 SourceLocation RParenLoc) {
Steve Naroff6d40db02007-10-31 18:42:27 +00001972 QualType t = Context.getObjcSelType();
Fariborz Jahanian4bef4622007-10-16 20:40:23 +00001973 return new ObjCSelectorExpr(t, Sel, AtLoc, RParenLoc);
1974}
1975
Fariborz Jahaniana32aaef2007-10-17 16:58:11 +00001976Sema::ExprResult Sema::ParseObjCProtocolExpression(IdentifierInfo *ProtocolId,
1977 SourceLocation AtLoc,
1978 SourceLocation ProtoLoc,
1979 SourceLocation LParenLoc,
1980 SourceLocation RParenLoc) {
1981 ObjcProtocolDecl* PDecl = ObjcProtocols[ProtocolId];
1982 if (!PDecl) {
1983 Diag(ProtoLoc, diag::err_undeclared_protocol, ProtocolId->getName());
1984 return true;
1985 }
1986
1987 QualType t = GetObjcProtoType(AtLoc);
Fariborz Jahaniane183a822007-10-18 22:59:23 +00001988 if (t.isNull())
1989 return true;
Fariborz Jahaniana32aaef2007-10-17 16:58:11 +00001990 return new ObjCProtocolExpr(t, PDecl, AtLoc, RParenLoc);
1991}
Steve Naroff077c83b2007-10-16 23:12:48 +00001992
1993bool Sema::CheckMessageArgumentTypes(Expr **Args, unsigned NumArgs,
1994 ObjcMethodDecl *Method) {
1995 bool anyIncompatibleArgs = false;
1996
1997 for (unsigned i = 0; i < NumArgs; i++) {
1998 Expr *argExpr = Args[i];
1999 assert(argExpr && "CheckMessageArgumentTypes(): missing expression");
2000
2001 QualType lhsType = Method->getParamDecl(i)->getType();
2002 QualType rhsType = argExpr->getType();
2003
2004 // If necessary, apply function/array conversion. C99 6.7.5.3p[7,8].
2005 if (const ArrayType *ary = lhsType->getAsArrayType())
2006 lhsType = Context.getPointerType(ary->getElementType());
2007 else if (lhsType->isFunctionType())
2008 lhsType = Context.getPointerType(lhsType);
2009
2010 AssignmentCheckResult result = CheckSingleAssignmentConstraints(lhsType,
2011 argExpr);
2012 if (Args[i] != argExpr) // The expression was converted.
2013 Args[i] = argExpr; // Make sure we store the converted expression.
2014 SourceLocation l = argExpr->getLocStart();
2015
2016 // decode the result (notice that AST's are still created for extensions).
2017 switch (result) {
2018 case Compatible:
2019 break;
2020 case PointerFromInt:
2021 // check for null pointer constant (C99 6.3.2.3p3)
2022 if (!argExpr->isNullPointerConstant(Context)) {
2023 Diag(l, diag::ext_typecheck_sending_pointer_int,
2024 lhsType.getAsString(), rhsType.getAsString(),
2025 argExpr->getSourceRange());
2026 }
2027 break;
2028 case IntFromPointer:
2029 Diag(l, diag::ext_typecheck_sending_pointer_int,
2030 lhsType.getAsString(), rhsType.getAsString(),
2031 argExpr->getSourceRange());
2032 break;
2033 case IncompatiblePointer:
2034 Diag(l, diag::ext_typecheck_sending_incompatible_pointer,
2035 rhsType.getAsString(), lhsType.getAsString(),
2036 argExpr->getSourceRange());
2037 break;
2038 case CompatiblePointerDiscardsQualifiers:
2039 Diag(l, diag::ext_typecheck_passing_discards_qualifiers,
2040 rhsType.getAsString(), lhsType.getAsString(),
2041 argExpr->getSourceRange());
2042 break;
2043 case Incompatible:
2044 Diag(l, diag::err_typecheck_sending_incompatible,
2045 rhsType.getAsString(), lhsType.getAsString(),
2046 argExpr->getSourceRange());
2047 anyIncompatibleArgs = true;
2048 }
2049 }
2050 return anyIncompatibleArgs;
2051}
2052
Steve Narofff73590d2007-09-27 14:38:14 +00002053// ActOnClassMessage - used for both unary and keyword messages.
2054// ArgExprs is optional - if it is present, the number of expressions
2055// is obtained from Sel.getNumArgs().
2056Sema::ExprResult Sema::ActOnClassMessage(
Steve Naroffc6814ea2007-10-02 20:01:56 +00002057 IdentifierInfo *receiverName, Selector Sel,
Steve Narofff73590d2007-09-27 14:38:14 +00002058 SourceLocation lbrac, SourceLocation rbrac, ExprTy **Args)
Steve Narofff0f2afc2007-09-17 21:01:15 +00002059{
Steve Naroffc6814ea2007-10-02 20:01:56 +00002060 assert(receiverName && "missing receiver class name");
Steve Naroffd54978b2007-09-18 23:55:05 +00002061
Steve Naroff077c83b2007-10-16 23:12:48 +00002062 Expr **ArgExprs = reinterpret_cast<Expr **>(Args);
Steve Naroffc6814ea2007-10-02 20:01:56 +00002063 ObjcInterfaceDecl* ClassDecl = getObjCInterfaceDecl(receiverName);
2064 ObjcMethodDecl *Method = ClassDecl->lookupClassMethod(Sel);
Steve Naroff55f52da2007-10-16 20:39:36 +00002065 QualType returnType;
Steve Naroff96d20612007-11-05 15:27:52 +00002066
2067 // Before we give up, check if the selector is an instance method.
2068 if (!Method)
2069 Method = ClassDecl->lookupInstanceMethod(Sel);
Steve Naroff55f52da2007-10-16 20:39:36 +00002070 if (!Method) {
2071 Diag(lbrac, diag::warn_method_not_found, std::string("+"), Sel.getName(),
2072 SourceRange(lbrac, rbrac));
Steve Naroff6d40db02007-10-31 18:42:27 +00002073 returnType = Context.getObjcIdType();
Steve Naroff55f52da2007-10-16 20:39:36 +00002074 } else {
Steve Naroffd2754262007-10-16 21:36:54 +00002075 returnType = Method->getResultType();
Steve Naroff077c83b2007-10-16 23:12:48 +00002076 if (Sel.getNumArgs()) {
2077 if (CheckMessageArgumentTypes(ArgExprs, Sel.getNumArgs(), Method))
2078 return true;
2079 }
Steve Naroff55f52da2007-10-16 20:39:36 +00002080 }
Steve Naroff66697aa2007-11-03 16:37:59 +00002081 return new ObjCMessageExpr(receiverName, Sel, returnType, Method,
2082 lbrac, rbrac, ArgExprs);
Steve Narofff0f2afc2007-09-17 21:01:15 +00002083}
2084
Steve Narofff73590d2007-09-27 14:38:14 +00002085// ActOnInstanceMessage - used for both unary and keyword messages.
2086// ArgExprs is optional - if it is present, the number of expressions
2087// is obtained from Sel.getNumArgs().
2088Sema::ExprResult Sema::ActOnInstanceMessage(
Steve Naroff80175062007-09-28 22:22:11 +00002089 ExprTy *receiver, Selector Sel,
Steve Narofff73590d2007-09-27 14:38:14 +00002090 SourceLocation lbrac, SourceLocation rbrac, ExprTy **Args)
2091{
Steve Naroffd54978b2007-09-18 23:55:05 +00002092 assert(receiver && "missing receiver expression");
2093
Steve Naroff077c83b2007-10-16 23:12:48 +00002094 Expr **ArgExprs = reinterpret_cast<Expr **>(Args);
Steve Naroffd54978b2007-09-18 23:55:05 +00002095 Expr *RExpr = static_cast<Expr *>(receiver);
Steve Naroffc6814ea2007-10-02 20:01:56 +00002096 QualType receiverType = RExpr->getType();
Steve Naroff7f549f12007-10-10 21:53:07 +00002097 QualType returnType;
Steve Naroff66697aa2007-11-03 16:37:59 +00002098 ObjcMethodDecl *Method;
Steve Naroff7f549f12007-10-10 21:53:07 +00002099
Steve Naroff6d40db02007-10-31 18:42:27 +00002100 if (receiverType == Context.getObjcIdType()) {
Steve Naroff66697aa2007-11-03 16:37:59 +00002101 Method = InstanceMethodPool[Sel].Method;
Steve Naroff55f52da2007-10-16 20:39:36 +00002102 if (!Method) {
2103 Diag(lbrac, diag::warn_method_not_found, std::string("-"), Sel.getName(),
2104 SourceRange(lbrac, rbrac));
Steve Naroff6d40db02007-10-31 18:42:27 +00002105 returnType = Context.getObjcIdType();
Steve Naroff55f52da2007-10-16 20:39:36 +00002106 } else {
Steve Naroffd2754262007-10-16 21:36:54 +00002107 returnType = Method->getResultType();
Steve Naroff077c83b2007-10-16 23:12:48 +00002108 if (Sel.getNumArgs())
2109 if (CheckMessageArgumentTypes(ArgExprs, Sel.getNumArgs(), Method))
2110 return true;
Steve Naroff55f52da2007-10-16 20:39:36 +00002111 }
Steve Naroff7f549f12007-10-10 21:53:07 +00002112 } else {
Chris Lattner9c7a0362007-10-10 23:42:28 +00002113 // FIXME (snaroff): checking in this code from Patrick. Needs to be
2114 // revisited. how do we get the ClassDecl from the receiver expression?
Steve Naroff7f549f12007-10-10 21:53:07 +00002115 while (receiverType->isPointerType()) {
Chris Lattner9c7a0362007-10-10 23:42:28 +00002116 PointerType *pointerType =
2117 static_cast<PointerType*>(receiverType.getTypePtr());
Steve Naroff7f549f12007-10-10 21:53:07 +00002118 receiverType = pointerType->getPointeeType();
2119 }
Chris Lattner9c7a0362007-10-10 23:42:28 +00002120 assert(ObjcInterfaceType::classof(receiverType.getTypePtr()) &&
2121 "bad receiver type");
Steve Naroff7f549f12007-10-10 21:53:07 +00002122 ObjcInterfaceDecl* ClassDecl = static_cast<ObjcInterfaceType*>(
2123 receiverType.getTypePtr())->getDecl();
Steve Naroff55f52da2007-10-16 20:39:36 +00002124 // FIXME: consider using InstanceMethodPool, since it will be faster
2125 // than the following method (which can do *many* linear searches). The
2126 // idea is to add class info to InstanceMethodPool...
Steve Naroff66697aa2007-11-03 16:37:59 +00002127 Method = ClassDecl->lookupInstanceMethod(Sel);
Steve Naroff55f52da2007-10-16 20:39:36 +00002128 if (!Method) {
Steve Naroff22e078e2007-11-11 00:10:47 +00002129 // If we have an implementation in scope, check "private" methods.
2130 if (ObjcImplementationDecl *ImpDecl =
2131 ObjcImplementations[ClassDecl->getIdentifier()])
2132 Method = ImpDecl->lookupInstanceMethod(Sel);
2133 }
2134 if (!Method) {
Steve Naroff55f52da2007-10-16 20:39:36 +00002135 Diag(lbrac, diag::warn_method_not_found, std::string("-"), Sel.getName(),
2136 SourceRange(lbrac, rbrac));
Steve Naroff6d40db02007-10-31 18:42:27 +00002137 returnType = Context.getObjcIdType();
Steve Naroff55f52da2007-10-16 20:39:36 +00002138 } else {
Steve Naroffd2754262007-10-16 21:36:54 +00002139 returnType = Method->getResultType();
Steve Naroff077c83b2007-10-16 23:12:48 +00002140 if (Sel.getNumArgs())
2141 if (CheckMessageArgumentTypes(ArgExprs, Sel.getNumArgs(), Method))
2142 return true;
Steve Naroff55f52da2007-10-16 20:39:36 +00002143 }
Steve Naroffc6814ea2007-10-02 20:01:56 +00002144 }
Steve Naroff66697aa2007-11-03 16:37:59 +00002145 return new ObjCMessageExpr(RExpr, Sel, returnType, Method, lbrac, rbrac,
2146 ArgExprs);
Steve Narofff0f2afc2007-09-17 21:01:15 +00002147}