blob: 32d7a6086ddb58659af469185da82aa8fc1caa5e [file] [log] [blame]
Chris Lattner4b009652007-07-25 00:24:17 +00001//===--- SemaExpr.cpp - Semantic Analysis for Expressions -----------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner959e5be2007-12-29 19:59:25 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Chris Lattner4b009652007-07-25 00:24:17 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file implements semantic analysis for expressions.
11//
12//===----------------------------------------------------------------------===//
13
14#include "Sema.h"
Ted Kremenek30c66752007-11-25 00:58:00 +000015#include "SemaUtil.h"
Chris Lattner4b009652007-07-25 00:24:17 +000016#include "clang/AST/ASTContext.h"
Chris Lattner4b009652007-07-25 00:24:17 +000017#include "clang/AST/Expr.h"
Chris Lattner3e254fb2008-04-08 04:40:51 +000018#include "clang/AST/ExprCXX.h"
Steve Naroff9ed3e772008-05-29 21:12:08 +000019#include "clang/AST/ExprObjC.h"
Steve Naroffc39ca262007-09-18 23:55:05 +000020#include "clang/Parse/DeclSpec.h"
Chris Lattner4b009652007-07-25 00:24:17 +000021#include "clang/Lex/Preprocessor.h"
22#include "clang/Lex/LiteralSupport.h"
23#include "clang/Basic/SourceManager.h"
Chris Lattner4b009652007-07-25 00:24:17 +000024#include "clang/Basic/TargetInfo.h"
Chris Lattner83bd5eb2007-12-28 05:29:59 +000025#include "llvm/ADT/OwningPtr.h"
Chris Lattner4b009652007-07-25 00:24:17 +000026#include "llvm/ADT/SmallString.h"
Chris Lattner2e64c072007-08-10 20:18:51 +000027#include "llvm/ADT/StringExtras.h"
Chris Lattner4b009652007-07-25 00:24:17 +000028using namespace clang;
29
Steve Naroff87d58b42007-09-16 03:34:24 +000030/// ActOnStringLiteral - The specified tokens were lexed as pasted string
Chris Lattner4b009652007-07-25 00:24:17 +000031/// fragments (e.g. "foo" "bar" L"baz"). The result string has to handle string
32/// concatenation ([C99 5.1.1.2, translation phase #6]), so it may come from
33/// multiple tokens. However, the common case is that StringToks points to one
34/// string.
35///
36Action::ExprResult
Steve Naroff87d58b42007-09-16 03:34:24 +000037Sema::ActOnStringLiteral(const Token *StringToks, unsigned NumStringToks) {
Chris Lattner4b009652007-07-25 00:24:17 +000038 assert(NumStringToks && "Must have at least one string!");
39
40 StringLiteralParser Literal(StringToks, NumStringToks, PP, Context.Target);
41 if (Literal.hadError)
42 return ExprResult(true);
43
44 llvm::SmallVector<SourceLocation, 4> StringTokLocs;
45 for (unsigned i = 0; i != NumStringToks; ++i)
46 StringTokLocs.push_back(StringToks[i].getLocation());
Chris Lattnera6dcce32008-02-11 00:02:17 +000047
48 // Verify that pascal strings aren't too large.
Anders Carlsson55bfe0d2007-10-15 02:50:23 +000049 if (Literal.Pascal && Literal.GetStringLength() > 256)
50 return Diag(StringToks[0].getLocation(), diag::err_pascal_string_too_long,
51 SourceRange(StringToks[0].getLocation(),
52 StringToks[NumStringToks-1].getLocation()));
Chris Lattner4b009652007-07-25 00:24:17 +000053
Chris Lattnera6dcce32008-02-11 00:02:17 +000054 QualType StrTy = Context.CharTy;
Eli Friedman256b7d72008-05-27 07:57:14 +000055 if (Literal.AnyWide) StrTy = Context.getWcharType();
Chris Lattnera6dcce32008-02-11 00:02:17 +000056 if (Literal.Pascal) StrTy = Context.UnsignedCharTy;
57
58 // Get an array type for the string, according to C99 6.4.5. This includes
59 // the nul terminator character as well as the string length for pascal
60 // strings.
61 StrTy = Context.getConstantArrayType(StrTy,
62 llvm::APInt(32, Literal.GetStringLength()+1),
63 ArrayType::Normal, 0);
64
Chris Lattner4b009652007-07-25 00:24:17 +000065 // Pass &StringTokLocs[0], StringTokLocs.size() to factory!
66 return new StringLiteral(Literal.GetString(), Literal.GetStringLength(),
Chris Lattnera6dcce32008-02-11 00:02:17 +000067 Literal.AnyWide, StrTy,
Anders Carlsson55bfe0d2007-10-15 02:50:23 +000068 StringToks[0].getLocation(),
Chris Lattner4b009652007-07-25 00:24:17 +000069 StringToks[NumStringToks-1].getLocation());
70}
71
72
Steve Naroff0acc9c92007-09-15 18:49:24 +000073/// ActOnIdentifierExpr - The parser read an identifier in expression context,
Chris Lattner4b009652007-07-25 00:24:17 +000074/// validate it per-C99 6.5.1. HasTrailingLParen indicates whether this
Steve Naroffe50e14c2008-03-19 23:46:26 +000075/// identifier is used in a function call context.
Steve Naroff0acc9c92007-09-15 18:49:24 +000076Sema::ExprResult Sema::ActOnIdentifierExpr(Scope *S, SourceLocation Loc,
Chris Lattner4b009652007-07-25 00:24:17 +000077 IdentifierInfo &II,
78 bool HasTrailingLParen) {
Chris Lattnerc72d22d2008-03-31 00:36:02 +000079 // Could be enum-constant, value decl, instance variable, etc.
Steve Naroff6384a012008-04-02 14:35:35 +000080 Decl *D = LookupDecl(&II, Decl::IDNS_Ordinary, S);
Chris Lattnerc72d22d2008-03-31 00:36:02 +000081
82 // If this reference is in an Objective-C method, then ivar lookup happens as
83 // well.
Argiris Kirtzidis95256e62008-06-28 06:07:14 +000084 if (getCurMethodDecl()) {
Steve Naroffe57c21a2008-04-01 23:04:06 +000085 ScopedDecl *SD = dyn_cast_or_null<ScopedDecl>(D);
Chris Lattnerc72d22d2008-03-31 00:36:02 +000086 // There are two cases to handle here. 1) scoped lookup could have failed,
87 // in which case we should look for an ivar. 2) scoped lookup could have
88 // found a decl, but that decl is outside the current method (i.e. a global
89 // variable). In these two cases, we do a lookup for an ivar with this
90 // name, if the lookup suceeds, we replace it our current decl.
Steve Naroffe57c21a2008-04-01 23:04:06 +000091 if (SD == 0 || SD->isDefinedOutsideFunctionOrMethod()) {
Argiris Kirtzidis95256e62008-06-28 06:07:14 +000092 ObjCInterfaceDecl *IFace = getCurMethodDecl()->getClassInterface();
93 ObjCInterfaceDecl *DeclClass;
Chris Lattnerc72d22d2008-03-31 00:36:02 +000094 if (ObjCIvarDecl *IV = IFace->lookupInstanceVariable(&II, DeclClass)) {
95 // FIXME: This should use a new expr for a direct reference, don't turn
96 // this into Self->ivar, just return a BareIVarExpr or something.
97 IdentifierInfo &II = Context.Idents.get("self");
98 ExprResult SelfExpr = ActOnIdentifierExpr(S, Loc, II, false);
99 return new ObjCIvarRefExpr(IV, IV->getType(), Loc,
100 static_cast<Expr*>(SelfExpr.Val), true, true);
101 }
102 }
Steve Naroffe90d4cc2008-06-05 18:14:25 +0000103 if (SD == 0 && !strcmp(II.getName(), "super")) {
Steve Naroff6f786252008-06-02 23:03:37 +0000104 QualType T = Context.getPointerType(Context.getObjCInterfaceType(
Argiris Kirtzidis95256e62008-06-28 06:07:14 +0000105 getCurMethodDecl()->getClassInterface()));
Steve Naroff6f786252008-06-02 23:03:37 +0000106 return new ObjCSuperRefExpr(T, Loc);
107 }
Chris Lattnerc72d22d2008-03-31 00:36:02 +0000108 }
109
Chris Lattner4b009652007-07-25 00:24:17 +0000110 if (D == 0) {
111 // Otherwise, this could be an implicitly declared function reference (legal
112 // in C90, extension in C99).
113 if (HasTrailingLParen &&
Chris Lattnerc72d22d2008-03-31 00:36:02 +0000114 !getLangOptions().CPlusPlus) // Not in C++.
Chris Lattner4b009652007-07-25 00:24:17 +0000115 D = ImplicitlyDefineFunction(Loc, II, S);
116 else {
117 // If this name wasn't predeclared and if this is not a function call,
118 // diagnose the problem.
119 return Diag(Loc, diag::err_undeclared_var_use, II.getName());
120 }
121 }
Chris Lattnerc72d22d2008-03-31 00:36:02 +0000122
Steve Naroff91b03f72007-08-28 03:03:08 +0000123 if (ValueDecl *VD = dyn_cast<ValueDecl>(D)) {
Chris Lattneree4c3bf2008-02-29 16:48:43 +0000124 // check if referencing an identifier with __attribute__((deprecated)).
125 if (VD->getAttr<DeprecatedAttr>())
126 Diag(Loc, diag::warn_deprecated, VD->getName());
127
Steve Naroffcae537d2007-08-28 18:45:29 +0000128 // Only create DeclRefExpr's for valid Decl's.
Steve Naroffd1ad6ae2007-08-28 20:14:24 +0000129 if (VD->isInvalidDecl())
Steve Naroff91b03f72007-08-28 03:03:08 +0000130 return true;
Chris Lattner4b009652007-07-25 00:24:17 +0000131 return new DeclRefExpr(VD, VD->getType(), Loc);
Steve Naroff91b03f72007-08-28 03:03:08 +0000132 }
Chris Lattnerc72d22d2008-03-31 00:36:02 +0000133
Chris Lattner4b009652007-07-25 00:24:17 +0000134 if (isa<TypedefDecl>(D))
135 return Diag(Loc, diag::err_unexpected_typedef, II.getName());
Ted Kremenek42730c52008-01-07 19:49:32 +0000136 if (isa<ObjCInterfaceDecl>(D))
Fariborz Jahanian3102df92007-12-05 18:16:33 +0000137 return Diag(Loc, diag::err_unexpected_interface, II.getName());
Argiris Kirtzidis03e6aaf2008-04-27 13:50:30 +0000138 if (isa<NamespaceDecl>(D))
139 return Diag(Loc, diag::err_unexpected_namespace, II.getName());
Chris Lattner4b009652007-07-25 00:24:17 +0000140
141 assert(0 && "Invalid decl");
142 abort();
143}
144
Steve Naroff87d58b42007-09-16 03:34:24 +0000145Sema::ExprResult Sema::ActOnPreDefinedExpr(SourceLocation Loc,
Chris Lattner4b009652007-07-25 00:24:17 +0000146 tok::TokenKind Kind) {
147 PreDefinedExpr::IdentType IT;
148
149 switch (Kind) {
Chris Lattnere12ca5d2008-01-12 18:39:25 +0000150 default: assert(0 && "Unknown simple primary expr!");
151 case tok::kw___func__: IT = PreDefinedExpr::Func; break; // [C99 6.4.2.2]
152 case tok::kw___FUNCTION__: IT = PreDefinedExpr::Function; break;
153 case tok::kw___PRETTY_FUNCTION__: IT = PreDefinedExpr::PrettyFunction; break;
Chris Lattner4b009652007-07-25 00:24:17 +0000154 }
Chris Lattnere12ca5d2008-01-12 18:39:25 +0000155
156 // Verify that this is in a function context.
Argiris Kirtzidis95256e62008-06-28 06:07:14 +0000157 if (getCurFunctionDecl() == 0 && getCurMethodDecl() == 0)
Chris Lattnere12ca5d2008-01-12 18:39:25 +0000158 return Diag(Loc, diag::err_predef_outside_function);
Chris Lattner4b009652007-07-25 00:24:17 +0000159
Chris Lattner7e637512008-01-12 08:14:25 +0000160 // Pre-defined identifiers are of type char[x], where x is the length of the
161 // string.
Chris Lattnerfc9511c2008-01-12 19:32:28 +0000162 unsigned Length;
Argiris Kirtzidis95256e62008-06-28 06:07:14 +0000163 if (getCurFunctionDecl())
164 Length = getCurFunctionDecl()->getIdentifier()->getLength();
Chris Lattnerfc9511c2008-01-12 19:32:28 +0000165 else
Argiris Kirtzidis95256e62008-06-28 06:07:14 +0000166 Length = getCurMethodDecl()->getSynthesizedMethodSize();
Chris Lattnere12ca5d2008-01-12 18:39:25 +0000167
Chris Lattnerfc9511c2008-01-12 19:32:28 +0000168 llvm::APInt LengthI(32, Length + 1);
Chris Lattnere12ca5d2008-01-12 18:39:25 +0000169 QualType ResTy = Context.CharTy.getQualifiedType(QualType::Const);
Chris Lattnerfc9511c2008-01-12 19:32:28 +0000170 ResTy = Context.getConstantArrayType(ResTy, LengthI, ArrayType::Normal, 0);
Chris Lattner7e637512008-01-12 08:14:25 +0000171 return new PreDefinedExpr(Loc, ResTy, IT);
Chris Lattner4b009652007-07-25 00:24:17 +0000172}
173
Steve Naroff87d58b42007-09-16 03:34:24 +0000174Sema::ExprResult Sema::ActOnCharacterConstant(const Token &Tok) {
Chris Lattner4b009652007-07-25 00:24:17 +0000175 llvm::SmallString<16> CharBuffer;
176 CharBuffer.resize(Tok.getLength());
177 const char *ThisTokBegin = &CharBuffer[0];
178 unsigned ActualLength = PP.getSpelling(Tok, ThisTokBegin);
179
180 CharLiteralParser Literal(ThisTokBegin, ThisTokBegin+ActualLength,
181 Tok.getLocation(), PP);
182 if (Literal.hadError())
183 return ExprResult(true);
Chris Lattner6b22fb72008-03-01 08:32:21 +0000184
185 QualType type = getLangOptions().CPlusPlus ? Context.CharTy : Context.IntTy;
186
Chris Lattner1aaf71c2008-06-07 22:35:38 +0000187 return new CharacterLiteral(Literal.getValue(), Literal.isWide(), type,
188 Tok.getLocation());
Chris Lattner4b009652007-07-25 00:24:17 +0000189}
190
Steve Naroff87d58b42007-09-16 03:34:24 +0000191Action::ExprResult Sema::ActOnNumericConstant(const Token &Tok) {
Chris Lattner4b009652007-07-25 00:24:17 +0000192 // fast path for a single digit (which is quite common). A single digit
193 // cannot have a trigraph, escaped newline, radix prefix, or type suffix.
194 if (Tok.getLength() == 1) {
Chris Lattner48d7f382008-04-02 04:24:33 +0000195 const char *Ty = PP.getSourceManager().getCharacterData(Tok.getLocation());
Chris Lattner4b009652007-07-25 00:24:17 +0000196
Chris Lattner8cd0e932008-03-05 18:54:05 +0000197 unsigned IntSize =static_cast<unsigned>(Context.getTypeSize(Context.IntTy));
Chris Lattner48d7f382008-04-02 04:24:33 +0000198 return ExprResult(new IntegerLiteral(llvm::APInt(IntSize, *Ty-'0'),
Chris Lattner4b009652007-07-25 00:24:17 +0000199 Context.IntTy,
200 Tok.getLocation()));
201 }
202 llvm::SmallString<512> IntegerBuffer;
203 IntegerBuffer.resize(Tok.getLength());
204 const char *ThisTokBegin = &IntegerBuffer[0];
205
206 // Get the spelling of the token, which eliminates trigraphs, etc.
207 unsigned ActualLength = PP.getSpelling(Tok, ThisTokBegin);
208 NumericLiteralParser Literal(ThisTokBegin, ThisTokBegin+ActualLength,
209 Tok.getLocation(), PP);
210 if (Literal.hadError)
211 return ExprResult(true);
212
Chris Lattner1de66eb2007-08-26 03:42:43 +0000213 Expr *Res;
214
215 if (Literal.isFloatingLiteral()) {
Chris Lattner858eece2007-09-22 18:29:59 +0000216 QualType Ty;
Chris Lattner2a674dc2008-06-30 18:32:54 +0000217 if (Literal.isFloat)
Chris Lattner858eece2007-09-22 18:29:59 +0000218 Ty = Context.FloatTy;
Chris Lattner2a674dc2008-06-30 18:32:54 +0000219 else if (!Literal.isLong)
Chris Lattner858eece2007-09-22 18:29:59 +0000220 Ty = Context.DoubleTy;
Chris Lattner2a674dc2008-06-30 18:32:54 +0000221 else
Chris Lattnerfc18dcc2008-03-08 08:52:55 +0000222 Ty = Context.LongDoubleTy;
Chris Lattner2a674dc2008-06-30 18:32:54 +0000223
224 const llvm::fltSemantics &Format = Context.getFloatTypeSemantics(Ty);
225
Ted Kremenekddedbe22007-11-29 00:56:49 +0000226 // isExact will be set by GetFloatValue().
227 bool isExact = false;
Chris Lattner2a674dc2008-06-30 18:32:54 +0000228 Res = new FloatingLiteral(Literal.GetFloatValue(Format, &isExact), &isExact,
Ted Kremenekddedbe22007-11-29 00:56:49 +0000229 Ty, Tok.getLocation());
230
Chris Lattner1de66eb2007-08-26 03:42:43 +0000231 } else if (!Literal.isIntegerLiteral()) {
232 return ExprResult(true);
233 } else {
Chris Lattner48d7f382008-04-02 04:24:33 +0000234 QualType Ty;
Chris Lattner4b009652007-07-25 00:24:17 +0000235
Neil Booth7421e9c2007-08-29 22:00:19 +0000236 // long long is a C99 feature.
237 if (!getLangOptions().C99 && !getLangOptions().CPlusPlus0x &&
Neil Booth9bd47082007-08-29 22:13:52 +0000238 Literal.isLongLong)
Neil Booth7421e9c2007-08-29 22:00:19 +0000239 Diag(Tok.getLocation(), diag::ext_longlong);
240
Chris Lattner4b009652007-07-25 00:24:17 +0000241 // Get the value in the widest-possible width.
Chris Lattner8cd0e932008-03-05 18:54:05 +0000242 llvm::APInt ResultVal(Context.Target.getIntMaxTWidth(), 0);
Chris Lattner4b009652007-07-25 00:24:17 +0000243
244 if (Literal.GetIntegerValue(ResultVal)) {
245 // If this value didn't fit into uintmax_t, warn and force to ull.
246 Diag(Tok.getLocation(), diag::warn_integer_too_large);
Chris Lattner48d7f382008-04-02 04:24:33 +0000247 Ty = Context.UnsignedLongLongTy;
248 assert(Context.getTypeSize(Ty) == ResultVal.getBitWidth() &&
Chris Lattner8cd0e932008-03-05 18:54:05 +0000249 "long long is not intmax_t?");
Chris Lattner4b009652007-07-25 00:24:17 +0000250 } else {
251 // If this value fits into a ULL, try to figure out what else it fits into
252 // according to the rules of C99 6.4.4.1p5.
253
254 // Octal, Hexadecimal, and integers with a U suffix are allowed to
255 // be an unsigned int.
256 bool AllowUnsigned = Literal.isUnsigned || Literal.getRadix() != 10;
257
258 // Check from smallest to largest, picking the smallest type we can.
Chris Lattnere4068872008-05-09 05:59:00 +0000259 unsigned Width = 0;
Chris Lattner98540b62007-08-23 21:58:08 +0000260 if (!Literal.isLong && !Literal.isLongLong) {
261 // Are int/unsigned possibilities?
Chris Lattnere4068872008-05-09 05:59:00 +0000262 unsigned IntSize = Context.Target.getIntWidth();
263
Chris Lattner4b009652007-07-25 00:24:17 +0000264 // Does it fit in a unsigned int?
265 if (ResultVal.isIntN(IntSize)) {
266 // Does it fit in a signed int?
267 if (!Literal.isUnsigned && ResultVal[IntSize-1] == 0)
Chris Lattner48d7f382008-04-02 04:24:33 +0000268 Ty = Context.IntTy;
Chris Lattner4b009652007-07-25 00:24:17 +0000269 else if (AllowUnsigned)
Chris Lattner48d7f382008-04-02 04:24:33 +0000270 Ty = Context.UnsignedIntTy;
Chris Lattnere4068872008-05-09 05:59:00 +0000271 Width = IntSize;
Chris Lattner4b009652007-07-25 00:24:17 +0000272 }
Chris Lattner4b009652007-07-25 00:24:17 +0000273 }
274
275 // Are long/unsigned long possibilities?
Chris Lattner48d7f382008-04-02 04:24:33 +0000276 if (Ty.isNull() && !Literal.isLongLong) {
Chris Lattnere4068872008-05-09 05:59:00 +0000277 unsigned LongSize = Context.Target.getLongWidth();
Chris Lattner4b009652007-07-25 00:24:17 +0000278
279 // Does it fit in a unsigned long?
280 if (ResultVal.isIntN(LongSize)) {
281 // Does it fit in a signed long?
282 if (!Literal.isUnsigned && ResultVal[LongSize-1] == 0)
Chris Lattner48d7f382008-04-02 04:24:33 +0000283 Ty = Context.LongTy;
Chris Lattner4b009652007-07-25 00:24:17 +0000284 else if (AllowUnsigned)
Chris Lattner48d7f382008-04-02 04:24:33 +0000285 Ty = Context.UnsignedLongTy;
Chris Lattnere4068872008-05-09 05:59:00 +0000286 Width = LongSize;
Chris Lattner4b009652007-07-25 00:24:17 +0000287 }
Chris Lattner4b009652007-07-25 00:24:17 +0000288 }
289
290 // Finally, check long long if needed.
Chris Lattner48d7f382008-04-02 04:24:33 +0000291 if (Ty.isNull()) {
Chris Lattnere4068872008-05-09 05:59:00 +0000292 unsigned LongLongSize = Context.Target.getLongLongWidth();
Chris Lattner4b009652007-07-25 00:24:17 +0000293
294 // Does it fit in a unsigned long long?
295 if (ResultVal.isIntN(LongLongSize)) {
296 // Does it fit in a signed long long?
297 if (!Literal.isUnsigned && ResultVal[LongLongSize-1] == 0)
Chris Lattner48d7f382008-04-02 04:24:33 +0000298 Ty = Context.LongLongTy;
Chris Lattner4b009652007-07-25 00:24:17 +0000299 else if (AllowUnsigned)
Chris Lattner48d7f382008-04-02 04:24:33 +0000300 Ty = Context.UnsignedLongLongTy;
Chris Lattnere4068872008-05-09 05:59:00 +0000301 Width = LongLongSize;
Chris Lattner4b009652007-07-25 00:24:17 +0000302 }
303 }
304
305 // If we still couldn't decide a type, we probably have something that
306 // does not fit in a signed long long, but has no U suffix.
Chris Lattner48d7f382008-04-02 04:24:33 +0000307 if (Ty.isNull()) {
Chris Lattner4b009652007-07-25 00:24:17 +0000308 Diag(Tok.getLocation(), diag::warn_integer_too_large_for_signed);
Chris Lattner48d7f382008-04-02 04:24:33 +0000309 Ty = Context.UnsignedLongLongTy;
Chris Lattnere4068872008-05-09 05:59:00 +0000310 Width = Context.Target.getLongLongWidth();
Chris Lattner4b009652007-07-25 00:24:17 +0000311 }
Chris Lattnere4068872008-05-09 05:59:00 +0000312
313 if (ResultVal.getBitWidth() != Width)
314 ResultVal.trunc(Width);
Chris Lattner4b009652007-07-25 00:24:17 +0000315 }
316
Chris Lattner48d7f382008-04-02 04:24:33 +0000317 Res = new IntegerLiteral(ResultVal, Ty, Tok.getLocation());
Chris Lattner4b009652007-07-25 00:24:17 +0000318 }
Chris Lattner1de66eb2007-08-26 03:42:43 +0000319
320 // If this is an imaginary literal, create the ImaginaryLiteral wrapper.
321 if (Literal.isImaginary)
322 Res = new ImaginaryLiteral(Res, Context.getComplexType(Res->getType()));
323
324 return Res;
Chris Lattner4b009652007-07-25 00:24:17 +0000325}
326
Steve Naroff87d58b42007-09-16 03:34:24 +0000327Action::ExprResult Sema::ActOnParenExpr(SourceLocation L, SourceLocation R,
Chris Lattner4b009652007-07-25 00:24:17 +0000328 ExprTy *Val) {
Chris Lattner48d7f382008-04-02 04:24:33 +0000329 Expr *E = (Expr *)Val;
330 assert((E != 0) && "ActOnParenExpr() missing expr");
331 return new ParenExpr(L, R, E);
Chris Lattner4b009652007-07-25 00:24:17 +0000332}
333
334/// The UsualUnaryConversions() function is *not* called by this routine.
335/// See C99 6.3.2.1p[2-4] for more details.
336QualType Sema::CheckSizeOfAlignOfOperand(QualType exprType,
337 SourceLocation OpLoc, bool isSizeof) {
338 // C99 6.5.3.4p1:
339 if (isa<FunctionType>(exprType) && isSizeof)
340 // alignof(function) is allowed.
341 Diag(OpLoc, diag::ext_sizeof_function_type);
342 else if (exprType->isVoidType())
343 Diag(OpLoc, diag::ext_sizeof_void_type, isSizeof ? "sizeof" : "__alignof");
344 else if (exprType->isIncompleteType()) {
345 Diag(OpLoc, isSizeof ? diag::err_sizeof_incomplete_type :
346 diag::err_alignof_incomplete_type,
347 exprType.getAsString());
348 return QualType(); // error
349 }
350 // C99 6.5.3.4p4: the type (an unsigned integer type) is size_t.
351 return Context.getSizeType();
352}
353
354Action::ExprResult Sema::
Steve Naroff87d58b42007-09-16 03:34:24 +0000355ActOnSizeOfAlignOfTypeExpr(SourceLocation OpLoc, bool isSizeof,
Chris Lattner4b009652007-07-25 00:24:17 +0000356 SourceLocation LPLoc, TypeTy *Ty,
357 SourceLocation RPLoc) {
358 // If error parsing type, ignore.
359 if (Ty == 0) return true;
360
361 // Verify that this is a valid expression.
362 QualType ArgTy = QualType::getFromOpaquePtr(Ty);
363
364 QualType resultType = CheckSizeOfAlignOfOperand(ArgTy, OpLoc, isSizeof);
365
366 if (resultType.isNull())
367 return true;
368 return new SizeOfAlignOfTypeExpr(isSizeof, ArgTy, resultType, OpLoc, RPLoc);
369}
370
Chris Lattner5110ad52007-08-24 21:41:10 +0000371QualType Sema::CheckRealImagOperand(Expr *&V, SourceLocation Loc) {
Chris Lattner03931a72007-08-24 21:16:53 +0000372 DefaultFunctionArrayConversion(V);
373
Chris Lattnera16e42d2007-08-26 05:39:26 +0000374 // These operators return the element type of a complex type.
Chris Lattner03931a72007-08-24 21:16:53 +0000375 if (const ComplexType *CT = V->getType()->getAsComplexType())
376 return CT->getElementType();
Chris Lattnera16e42d2007-08-26 05:39:26 +0000377
378 // Otherwise they pass through real integer and floating point types here.
379 if (V->getType()->isArithmeticType())
380 return V->getType();
381
382 // Reject anything else.
383 Diag(Loc, diag::err_realimag_invalid_type, V->getType().getAsString());
384 return QualType();
Chris Lattner03931a72007-08-24 21:16:53 +0000385}
386
387
Chris Lattner4b009652007-07-25 00:24:17 +0000388
Steve Naroff87d58b42007-09-16 03:34:24 +0000389Action::ExprResult Sema::ActOnPostfixUnaryOp(SourceLocation OpLoc,
Chris Lattner4b009652007-07-25 00:24:17 +0000390 tok::TokenKind Kind,
391 ExprTy *Input) {
392 UnaryOperator::Opcode Opc;
393 switch (Kind) {
394 default: assert(0 && "Unknown unary op!");
395 case tok::plusplus: Opc = UnaryOperator::PostInc; break;
396 case tok::minusminus: Opc = UnaryOperator::PostDec; break;
397 }
398 QualType result = CheckIncrementDecrementOperand((Expr *)Input, OpLoc);
399 if (result.isNull())
400 return true;
401 return new UnaryOperator((Expr *)Input, Opc, result, OpLoc);
402}
403
404Action::ExprResult Sema::
Steve Naroff87d58b42007-09-16 03:34:24 +0000405ActOnArraySubscriptExpr(ExprTy *Base, SourceLocation LLoc,
Chris Lattner4b009652007-07-25 00:24:17 +0000406 ExprTy *Idx, SourceLocation RLoc) {
407 Expr *LHSExp = static_cast<Expr*>(Base), *RHSExp = static_cast<Expr*>(Idx);
408
409 // Perform default conversions.
410 DefaultFunctionArrayConversion(LHSExp);
411 DefaultFunctionArrayConversion(RHSExp);
412
413 QualType LHSTy = LHSExp->getType(), RHSTy = RHSExp->getType();
414
415 // C99 6.5.2.1p2: the expression e1[e2] is by definition precisely equivalent
Chris Lattner0d9bcea2007-08-30 17:45:32 +0000416 // to the expression *((e1)+(e2)). This means the array "Base" may actually be
Chris Lattner4b009652007-07-25 00:24:17 +0000417 // in the subscript position. As a result, we need to derive the array base
418 // and index from the expression types.
419 Expr *BaseExpr, *IndexExpr;
420 QualType ResultType;
Chris Lattner7931f4a2007-07-31 16:53:04 +0000421 if (const PointerType *PTy = LHSTy->getAsPointerType()) {
Chris Lattner4b009652007-07-25 00:24:17 +0000422 BaseExpr = LHSExp;
423 IndexExpr = RHSExp;
424 // FIXME: need to deal with const...
425 ResultType = PTy->getPointeeType();
Chris Lattner7931f4a2007-07-31 16:53:04 +0000426 } else if (const PointerType *PTy = RHSTy->getAsPointerType()) {
Chris Lattner4b009652007-07-25 00:24:17 +0000427 // Handle the uncommon case of "123[Ptr]".
428 BaseExpr = RHSExp;
429 IndexExpr = LHSExp;
430 // FIXME: need to deal with const...
431 ResultType = PTy->getPointeeType();
Chris Lattnere35a1042007-07-31 19:29:30 +0000432 } else if (const VectorType *VTy = LHSTy->getAsVectorType()) {
433 BaseExpr = LHSExp; // vectors: V[123]
Chris Lattner4b009652007-07-25 00:24:17 +0000434 IndexExpr = RHSExp;
Steve Naroff89345522007-08-03 22:40:33 +0000435
436 // Component access limited to variables (reject vec4.rg[1]).
Nate Begemanc8e51f82008-05-09 06:41:27 +0000437 if (!isa<DeclRefExpr>(BaseExpr) && !isa<ArraySubscriptExpr>(BaseExpr) &&
438 !isa<ExtVectorElementExpr>(BaseExpr))
Nate Begemanaf6ed502008-04-18 23:10:10 +0000439 return Diag(LLoc, diag::err_ext_vector_component_access,
Steve Naroff89345522007-08-03 22:40:33 +0000440 SourceRange(LLoc, RLoc));
Chris Lattner4b009652007-07-25 00:24:17 +0000441 // FIXME: need to deal with const...
442 ResultType = VTy->getElementType();
443 } else {
444 return Diag(LHSExp->getLocStart(), diag::err_typecheck_subscript_value,
445 RHSExp->getSourceRange());
446 }
447 // C99 6.5.2.1p1
448 if (!IndexExpr->getType()->isIntegerType())
449 return Diag(IndexExpr->getLocStart(), diag::err_typecheck_subscript,
450 IndexExpr->getSourceRange());
451
452 // C99 6.5.2.1p1: "shall have type "pointer to *object* type". In practice,
453 // the following check catches trying to index a pointer to a function (e.g.
Chris Lattner9db553e2008-04-02 06:59:01 +0000454 // void (*)(int)) and pointers to incomplete types. Functions are not
455 // objects in C99.
Chris Lattner4b009652007-07-25 00:24:17 +0000456 if (!ResultType->isObjectType())
457 return Diag(BaseExpr->getLocStart(),
458 diag::err_typecheck_subscript_not_object,
459 BaseExpr->getType().getAsString(), BaseExpr->getSourceRange());
460
461 return new ArraySubscriptExpr(LHSExp, RHSExp, ResultType, RLoc);
462}
463
Steve Naroff1b8a46c2007-07-27 22:15:19 +0000464QualType Sema::
Nate Begemanaf6ed502008-04-18 23:10:10 +0000465CheckExtVectorComponent(QualType baseType, SourceLocation OpLoc,
Steve Naroff1b8a46c2007-07-27 22:15:19 +0000466 IdentifierInfo &CompName, SourceLocation CompLoc) {
Nate Begemanaf6ed502008-04-18 23:10:10 +0000467 const ExtVectorType *vecType = baseType->getAsExtVectorType();
Nate Begemanc8e51f82008-05-09 06:41:27 +0000468
469 // This flag determines whether or not the component is to be treated as a
470 // special name, or a regular GLSL-style component access.
471 bool SpecialComponent = false;
Steve Naroff1b8a46c2007-07-27 22:15:19 +0000472
473 // The vector accessor can't exceed the number of elements.
474 const char *compStr = CompName.getName();
475 if (strlen(compStr) > vecType->getNumElements()) {
Nate Begemanaf6ed502008-04-18 23:10:10 +0000476 Diag(OpLoc, diag::err_ext_vector_component_exceeds_length,
Steve Naroff1b8a46c2007-07-27 22:15:19 +0000477 baseType.getAsString(), SourceRange(CompLoc));
478 return QualType();
479 }
Nate Begemanc8e51f82008-05-09 06:41:27 +0000480
481 // Check that we've found one of the special components, or that the component
482 // names must come from the same set.
483 if (!strcmp(compStr, "hi") || !strcmp(compStr, "lo") ||
484 !strcmp(compStr, "e") || !strcmp(compStr, "o")) {
485 SpecialComponent = true;
486 } else if (vecType->getPointAccessorIdx(*compStr) != -1) {
Chris Lattner9096b792007-08-02 22:33:49 +0000487 do
488 compStr++;
489 while (*compStr && vecType->getPointAccessorIdx(*compStr) != -1);
490 } else if (vecType->getColorAccessorIdx(*compStr) != -1) {
491 do
492 compStr++;
493 while (*compStr && vecType->getColorAccessorIdx(*compStr) != -1);
494 } else if (vecType->getTextureAccessorIdx(*compStr) != -1) {
495 do
496 compStr++;
497 while (*compStr && vecType->getTextureAccessorIdx(*compStr) != -1);
498 }
Steve Naroff1b8a46c2007-07-27 22:15:19 +0000499
Nate Begemanc8e51f82008-05-09 06:41:27 +0000500 if (!SpecialComponent && *compStr) {
Steve Naroff1b8a46c2007-07-27 22:15:19 +0000501 // We didn't get to the end of the string. This means the component names
502 // didn't come from the same set *or* we encountered an illegal name.
Nate Begemanaf6ed502008-04-18 23:10:10 +0000503 Diag(OpLoc, diag::err_ext_vector_component_name_illegal,
Steve Naroff1b8a46c2007-07-27 22:15:19 +0000504 std::string(compStr,compStr+1), SourceRange(CompLoc));
505 return QualType();
506 }
507 // Each component accessor can't exceed the vector type.
508 compStr = CompName.getName();
509 while (*compStr) {
510 if (vecType->isAccessorWithinNumElements(*compStr))
511 compStr++;
512 else
513 break;
514 }
Nate Begemanc8e51f82008-05-09 06:41:27 +0000515 if (!SpecialComponent && *compStr) {
Steve Naroff1b8a46c2007-07-27 22:15:19 +0000516 // We didn't get to the end of the string. This means a component accessor
517 // exceeds the number of elements in the vector.
Nate Begemanaf6ed502008-04-18 23:10:10 +0000518 Diag(OpLoc, diag::err_ext_vector_component_exceeds_length,
Steve Naroff1b8a46c2007-07-27 22:15:19 +0000519 baseType.getAsString(), SourceRange(CompLoc));
520 return QualType();
521 }
Nate Begemanc8e51f82008-05-09 06:41:27 +0000522
523 // If we have a special component name, verify that the current vector length
524 // is an even number, since all special component names return exactly half
525 // the elements.
526 if (SpecialComponent && (vecType->getNumElements() & 1U)) {
527 return QualType();
528 }
529
Steve Naroff1b8a46c2007-07-27 22:15:19 +0000530 // The component accessor looks fine - now we need to compute the actual type.
531 // The vector type is implied by the component accessor. For example,
532 // vec4.b is a float, vec4.xy is a vec2, vec4.rgb is a vec3, etc.
Nate Begemanc8e51f82008-05-09 06:41:27 +0000533 // vec4.hi, vec4.lo, vec4.e, and vec4.o all return vec2.
534 unsigned CompSize = SpecialComponent ? vecType->getNumElements() / 2
535 : strlen(CompName.getName());
Steve Naroff1b8a46c2007-07-27 22:15:19 +0000536 if (CompSize == 1)
537 return vecType->getElementType();
Steve Naroff82113e32007-07-29 16:33:31 +0000538
Nate Begemanaf6ed502008-04-18 23:10:10 +0000539 QualType VT = Context.getExtVectorType(vecType->getElementType(), CompSize);
Steve Naroff82113e32007-07-29 16:33:31 +0000540 // Now look up the TypeDefDecl from the vector type. Without this,
Nate Begemanaf6ed502008-04-18 23:10:10 +0000541 // diagostics look bad. We want extended vector types to appear built-in.
542 for (unsigned i = 0, E = ExtVectorDecls.size(); i != E; ++i) {
543 if (ExtVectorDecls[i]->getUnderlyingType() == VT)
544 return Context.getTypedefType(ExtVectorDecls[i]);
Steve Naroff82113e32007-07-29 16:33:31 +0000545 }
546 return VT; // should never get here (a typedef type should always be found).
Steve Naroff1b8a46c2007-07-27 22:15:19 +0000547}
548
Chris Lattner4b009652007-07-25 00:24:17 +0000549Action::ExprResult Sema::
Steve Naroff87d58b42007-09-16 03:34:24 +0000550ActOnMemberReferenceExpr(ExprTy *Base, SourceLocation OpLoc,
Chris Lattner4b009652007-07-25 00:24:17 +0000551 tok::TokenKind OpKind, SourceLocation MemberLoc,
552 IdentifierInfo &Member) {
Steve Naroff2cb66382007-07-26 03:11:44 +0000553 Expr *BaseExpr = static_cast<Expr *>(Base);
554 assert(BaseExpr && "no record expression");
Steve Naroff137e11d2007-12-16 21:42:28 +0000555
556 // Perform default conversions.
557 DefaultFunctionArrayConversion(BaseExpr);
Chris Lattner4b009652007-07-25 00:24:17 +0000558
Steve Naroff2cb66382007-07-26 03:11:44 +0000559 QualType BaseType = BaseExpr->getType();
560 assert(!BaseType.isNull() && "no type for member expression");
Chris Lattner4b009652007-07-25 00:24:17 +0000561
Chris Lattner4b009652007-07-25 00:24:17 +0000562 if (OpKind == tok::arrow) {
Chris Lattner7931f4a2007-07-31 16:53:04 +0000563 if (const PointerType *PT = BaseType->getAsPointerType())
Steve Naroff2cb66382007-07-26 03:11:44 +0000564 BaseType = PT->getPointeeType();
565 else
566 return Diag(OpLoc, diag::err_typecheck_member_reference_arrow,
567 SourceRange(MemberLoc));
Chris Lattner4b009652007-07-25 00:24:17 +0000568 }
Nate Begemanaf6ed502008-04-18 23:10:10 +0000569 // The base type is either a record or an ExtVectorType.
Chris Lattnere35a1042007-07-31 19:29:30 +0000570 if (const RecordType *RTy = BaseType->getAsRecordType()) {
Steve Naroff2cb66382007-07-26 03:11:44 +0000571 RecordDecl *RDecl = RTy->getDecl();
572 if (RTy->isIncompleteType())
573 return Diag(OpLoc, diag::err_typecheck_incomplete_tag, RDecl->getName(),
574 BaseExpr->getSourceRange());
575 // The record definition is complete, now make sure the member is valid.
Steve Naroff1b8a46c2007-07-27 22:15:19 +0000576 FieldDecl *MemberDecl = RDecl->getMember(&Member);
577 if (!MemberDecl)
Steve Naroff2cb66382007-07-26 03:11:44 +0000578 return Diag(OpLoc, diag::err_typecheck_no_member, Member.getName(),
579 SourceRange(MemberLoc));
Eli Friedman76b49832008-02-06 22:48:16 +0000580
581 // Figure out the type of the member; see C99 6.5.2.3p3
Eli Friedmanaedabcf2008-02-07 05:24:51 +0000582 // FIXME: Handle address space modifiers
Eli Friedman76b49832008-02-06 22:48:16 +0000583 QualType MemberType = MemberDecl->getType();
584 unsigned combinedQualifiers =
Chris Lattner35fef522008-02-20 20:55:12 +0000585 MemberType.getCVRQualifiers() | BaseType.getCVRQualifiers();
Eli Friedman76b49832008-02-06 22:48:16 +0000586 MemberType = MemberType.getQualifiedType(combinedQualifiers);
587
588 return new MemberExpr(BaseExpr, OpKind==tok::arrow, MemberDecl,
589 MemberLoc, MemberType);
Nate Begemanaf6ed502008-04-18 23:10:10 +0000590 } else if (BaseType->isExtVectorType() && OpKind == tok::period) {
Steve Naroff89345522007-08-03 22:40:33 +0000591 // Component access limited to variables (reject vec4.rg.g).
Nate Begemanc8e51f82008-05-09 06:41:27 +0000592 if (!isa<DeclRefExpr>(BaseExpr) && !isa<ArraySubscriptExpr>(BaseExpr) &&
593 !isa<ExtVectorElementExpr>(BaseExpr))
Nate Begemanaf6ed502008-04-18 23:10:10 +0000594 return Diag(OpLoc, diag::err_ext_vector_component_access,
Steve Naroff89345522007-08-03 22:40:33 +0000595 SourceRange(MemberLoc));
Nate Begemanaf6ed502008-04-18 23:10:10 +0000596 QualType ret = CheckExtVectorComponent(BaseType, OpLoc, Member, MemberLoc);
Steve Naroff1b8a46c2007-07-27 22:15:19 +0000597 if (ret.isNull())
598 return true;
Nate Begemanaf6ed502008-04-18 23:10:10 +0000599 return new ExtVectorElementExpr(ret, BaseExpr, Member, MemberLoc);
Ted Kremenek42730c52008-01-07 19:49:32 +0000600 } else if (BaseType->isObjCInterfaceType()) {
601 ObjCInterfaceDecl *IFace;
Steve Naroffd1f0eb42008-06-05 13:55:23 +0000602 QualType CanonType = BaseType.getCanonicalType();
603 if (isa<ObjCInterfaceType>(CanonType))
604 IFace = dyn_cast<ObjCInterfaceType>(CanonType)->getDecl();
Fariborz Jahanian4af72492007-11-12 22:29:28 +0000605 else
Steve Naroffd1f0eb42008-06-05 13:55:23 +0000606 IFace = dyn_cast<ObjCQualifiedInterfaceType>(CanonType)->getDecl();
Ted Kremenek42730c52008-01-07 19:49:32 +0000607 ObjCInterfaceDecl *clsDeclared;
608 if (ObjCIvarDecl *IV = IFace->lookupInstanceVariable(&Member, clsDeclared))
Fariborz Jahanian4af72492007-11-12 22:29:28 +0000609 return new ObjCIvarRefExpr(IV, IV->getType(), MemberLoc, BaseExpr,
610 OpKind==tok::arrow);
Steve Naroff05391d22008-05-30 00:40:33 +0000611 } else if (isObjCObjectPointerType(BaseType)) {
612 PointerType *pointerType = static_cast<PointerType*>(BaseType.getTypePtr());
613 BaseType = pointerType->getPointeeType();
614 ObjCInterfaceDecl *IFace;
Steve Naroffd1f0eb42008-06-05 13:55:23 +0000615 QualType CanonType = BaseType.getCanonicalType();
616 if (isa<ObjCInterfaceType>(CanonType))
617 IFace = dyn_cast<ObjCInterfaceType>(CanonType)->getDecl();
Steve Naroff05391d22008-05-30 00:40:33 +0000618 else
Steve Naroffd1f0eb42008-06-05 13:55:23 +0000619 IFace = dyn_cast<ObjCQualifiedInterfaceType>(CanonType)->getDecl();
Steve Naroff05391d22008-05-30 00:40:33 +0000620 ObjCInterfaceDecl *clsDeclared;
621 if (ObjCIvarDecl *IV = IFace->lookupInstanceVariable(&Member, clsDeclared))
622 return new ObjCIvarRefExpr(IV, IV->getType(), MemberLoc, BaseExpr,
623 OpKind==tok::arrow);
624 // Check for properties.
625 if (OpKind==tok::period) {
626 // Before we look for explicit property declarations, we check for
627 // nullary methods (which allow '.' notation).
628 Selector Sel = PP.getSelectorTable().getNullarySelector(&Member);
629 ObjCMethodDecl *MD = IFace->lookupInstanceMethod(Sel);
630 if (MD)
631 return new ObjCPropertyRefExpr(MD, MD->getResultType(),
632 MemberLoc, BaseExpr);
633 // FIXME: Need to deal with setter methods that take 1 argument. E.g.:
634 // @interface NSBundle : NSObject {}
635 // - (NSString *)bundlePath;
636 // - (void)setBundlePath:(NSString *)x;
637 // @end
638 // void someMethod() { frameworkBundle.bundlePath = 0; }
639 //
Steve Naroff858813d2008-06-03 21:56:14 +0000640 ObjCPropertyDecl *PD = IFace->FindPropertyDeclaration(&Member);
Steve Naroffd1f0eb42008-06-05 13:55:23 +0000641
642 if (!PD) { // Lastly, check protocols on qualified interfaces.
643 if (ObjCQualifiedInterfaceType *QIT =
644 dyn_cast<ObjCQualifiedInterfaceType>(CanonType)) {
645 for (unsigned i = 0; i < QIT->getNumProtocols(); i++)
646 if ((PD = QIT->getProtocols(i)->FindPropertyDeclaration(&Member)))
647 break;
648 }
649 }
Steve Naroff858813d2008-06-03 21:56:14 +0000650 if (PD)
651 return new ObjCPropertyRefExpr(PD, PD->getType(), MemberLoc, BaseExpr);
Steve Naroff05391d22008-05-30 00:40:33 +0000652 }
Fariborz Jahanian4af72492007-11-12 22:29:28 +0000653 }
654 return Diag(OpLoc, diag::err_typecheck_member_reference_structUnion,
655 SourceRange(MemberLoc));
Chris Lattner4b009652007-07-25 00:24:17 +0000656}
657
Steve Naroff87d58b42007-09-16 03:34:24 +0000658/// ActOnCallExpr - Handle a call to Fn with the specified array of arguments.
Chris Lattner4b009652007-07-25 00:24:17 +0000659/// This provides the location of the left/right parens and a list of comma
660/// locations.
661Action::ExprResult Sema::
Steve Naroff87d58b42007-09-16 03:34:24 +0000662ActOnCallExpr(ExprTy *fn, SourceLocation LParenLoc,
Chris Lattner83bd5eb2007-12-28 05:29:59 +0000663 ExprTy **args, unsigned NumArgs,
Chris Lattner4b009652007-07-25 00:24:17 +0000664 SourceLocation *CommaLocs, SourceLocation RParenLoc) {
665 Expr *Fn = static_cast<Expr *>(fn);
666 Expr **Args = reinterpret_cast<Expr**>(args);
667 assert(Fn && "no function call expression");
Chris Lattner3e254fb2008-04-08 04:40:51 +0000668 FunctionDecl *FDecl = NULL;
Chris Lattner3e254fb2008-04-08 04:40:51 +0000669
670 // Promote the function operand.
671 UsualUnaryConversions(Fn);
672
673 // If we're directly calling a function, get the declaration for
674 // that function.
675 if (ImplicitCastExpr *IcExpr = dyn_cast<ImplicitCastExpr>(Fn))
676 if (DeclRefExpr *DRExpr = dyn_cast<DeclRefExpr>(IcExpr->getSubExpr()))
677 FDecl = dyn_cast<FunctionDecl>(DRExpr->getDecl());
678
Chris Lattner83bd5eb2007-12-28 05:29:59 +0000679 // Make the call expr early, before semantic checks. This guarantees cleanup
680 // of arguments and function on error.
Chris Lattner97316c02008-04-10 02:22:51 +0000681 llvm::OwningPtr<CallExpr> TheCall(new CallExpr(Fn, Args, NumArgs,
Chris Lattner83bd5eb2007-12-28 05:29:59 +0000682 Context.BoolTy, RParenLoc));
683
Chris Lattner4b009652007-07-25 00:24:17 +0000684 // C99 6.5.2.2p1 - "The expression that denotes the called function shall have
685 // type pointer to function".
Chris Lattner83bd5eb2007-12-28 05:29:59 +0000686 const PointerType *PT = Fn->getType()->getAsPointerType();
Chris Lattner4b009652007-07-25 00:24:17 +0000687 if (PT == 0)
688 return Diag(Fn->getLocStart(), diag::err_typecheck_call_not_function,
689 SourceRange(Fn->getLocStart(), RParenLoc));
Chris Lattner83bd5eb2007-12-28 05:29:59 +0000690 const FunctionType *FuncT = PT->getPointeeType()->getAsFunctionType();
691 if (FuncT == 0)
Chris Lattner4b009652007-07-25 00:24:17 +0000692 return Diag(Fn->getLocStart(), diag::err_typecheck_call_not_function,
693 SourceRange(Fn->getLocStart(), RParenLoc));
Chris Lattner83bd5eb2007-12-28 05:29:59 +0000694
695 // We know the result type of the call, set it.
696 TheCall->setType(FuncT->getResultType());
Chris Lattner4b009652007-07-25 00:24:17 +0000697
Chris Lattner83bd5eb2007-12-28 05:29:59 +0000698 if (const FunctionTypeProto *Proto = dyn_cast<FunctionTypeProto>(FuncT)) {
Chris Lattner4b009652007-07-25 00:24:17 +0000699 // C99 6.5.2.2p7 - the arguments are implicitly converted, as if by
700 // assignment, to the types of the corresponding parameter, ...
Chris Lattner83bd5eb2007-12-28 05:29:59 +0000701 unsigned NumArgsInProto = Proto->getNumArgs();
702 unsigned NumArgsToCheck = NumArgs;
Chris Lattner4b009652007-07-25 00:24:17 +0000703
Chris Lattner3e254fb2008-04-08 04:40:51 +0000704 // If too few arguments are available (and we don't have default
705 // arguments for the remaining parameters), don't make the call.
706 if (NumArgs < NumArgsInProto) {
Chris Lattner97316c02008-04-10 02:22:51 +0000707 if (FDecl && NumArgs >= FDecl->getMinRequiredArguments()) {
Chris Lattner3e254fb2008-04-08 04:40:51 +0000708 // Use default arguments for missing arguments
709 NumArgsToCheck = NumArgsInProto;
Chris Lattner97316c02008-04-10 02:22:51 +0000710 TheCall->setNumArgs(NumArgsInProto);
Chris Lattner3e254fb2008-04-08 04:40:51 +0000711 } else
712 return Diag(RParenLoc, diag::err_typecheck_call_too_few_args,
713 Fn->getSourceRange());
714 }
715
Chris Lattner83bd5eb2007-12-28 05:29:59 +0000716 // If too many are passed and not variadic, error on the extras and drop
717 // them.
718 if (NumArgs > NumArgsInProto) {
719 if (!Proto->isVariadic()) {
Chris Lattner4b009652007-07-25 00:24:17 +0000720 Diag(Args[NumArgsInProto]->getLocStart(),
721 diag::err_typecheck_call_too_many_args, Fn->getSourceRange(),
722 SourceRange(Args[NumArgsInProto]->getLocStart(),
Chris Lattner83bd5eb2007-12-28 05:29:59 +0000723 Args[NumArgs-1]->getLocEnd()));
724 // This deletes the extra arguments.
725 TheCall->setNumArgs(NumArgsInProto);
Chris Lattner4b009652007-07-25 00:24:17 +0000726 }
727 NumArgsToCheck = NumArgsInProto;
728 }
Chris Lattner83bd5eb2007-12-28 05:29:59 +0000729
Chris Lattner4b009652007-07-25 00:24:17 +0000730 // Continue to check argument types (even if we have too few/many args).
Chris Lattner83bd5eb2007-12-28 05:29:59 +0000731 for (unsigned i = 0; i != NumArgsToCheck; i++) {
Chris Lattner005ed752008-01-04 18:04:52 +0000732 QualType ProtoArgType = Proto->getArgType(i);
Chris Lattner3e254fb2008-04-08 04:40:51 +0000733
734 Expr *Arg;
735 if (i < NumArgs)
736 Arg = Args[i];
737 else
738 Arg = new CXXDefaultArgExpr(FDecl->getParamDecl(i));
Chris Lattner005ed752008-01-04 18:04:52 +0000739 QualType ArgType = Arg->getType();
Chris Lattner4b009652007-07-25 00:24:17 +0000740
Chris Lattner83bd5eb2007-12-28 05:29:59 +0000741 // Compute implicit casts from the operand to the formal argument type.
Chris Lattner005ed752008-01-04 18:04:52 +0000742 AssignConvertType ConvTy =
743 CheckSingleAssignmentConstraints(ProtoArgType, Arg);
Chris Lattner83bd5eb2007-12-28 05:29:59 +0000744 TheCall->setArg(i, Arg);
745
Chris Lattner005ed752008-01-04 18:04:52 +0000746 if (DiagnoseAssignmentResult(ConvTy, Arg->getLocStart(), ProtoArgType,
747 ArgType, Arg, "passing"))
748 return true;
Chris Lattner4b009652007-07-25 00:24:17 +0000749 }
Chris Lattner83bd5eb2007-12-28 05:29:59 +0000750
751 // If this is a variadic call, handle args passed through "...".
752 if (Proto->isVariadic()) {
Steve Naroffdb65e052007-08-28 23:30:39 +0000753 // Promote the arguments (C99 6.5.2.2p7).
Chris Lattner83bd5eb2007-12-28 05:29:59 +0000754 for (unsigned i = NumArgsInProto; i != NumArgs; i++) {
755 Expr *Arg = Args[i];
756 DefaultArgumentPromotion(Arg);
757 TheCall->setArg(i, Arg);
Steve Naroffdb65e052007-08-28 23:30:39 +0000758 }
Steve Naroffdb65e052007-08-28 23:30:39 +0000759 }
Chris Lattner83bd5eb2007-12-28 05:29:59 +0000760 } else {
761 assert(isa<FunctionTypeNoProto>(FuncT) && "Unknown FunctionType!");
762
Steve Naroffdb65e052007-08-28 23:30:39 +0000763 // Promote the arguments (C99 6.5.2.2p6).
Chris Lattner83bd5eb2007-12-28 05:29:59 +0000764 for (unsigned i = 0; i != NumArgs; i++) {
765 Expr *Arg = Args[i];
766 DefaultArgumentPromotion(Arg);
767 TheCall->setArg(i, Arg);
Steve Naroffdb65e052007-08-28 23:30:39 +0000768 }
Chris Lattner4b009652007-07-25 00:24:17 +0000769 }
Chris Lattner83bd5eb2007-12-28 05:29:59 +0000770
Chris Lattner2e64c072007-08-10 20:18:51 +0000771 // Do special checking on direct calls to functions.
Eli Friedmand0e9d092008-05-14 19:38:39 +0000772 if (FDecl)
773 return CheckFunctionCall(FDecl, TheCall.take());
Chris Lattner2e64c072007-08-10 20:18:51 +0000774
Chris Lattner83bd5eb2007-12-28 05:29:59 +0000775 return TheCall.take();
Chris Lattner4b009652007-07-25 00:24:17 +0000776}
777
778Action::ExprResult Sema::
Steve Naroff87d58b42007-09-16 03:34:24 +0000779ActOnCompoundLiteral(SourceLocation LParenLoc, TypeTy *Ty,
Chris Lattner4b009652007-07-25 00:24:17 +0000780 SourceLocation RParenLoc, ExprTy *InitExpr) {
Steve Naroff87d58b42007-09-16 03:34:24 +0000781 assert((Ty != 0) && "ActOnCompoundLiteral(): missing type");
Chris Lattner4b009652007-07-25 00:24:17 +0000782 QualType literalType = QualType::getFromOpaquePtr(Ty);
783 // FIXME: put back this assert when initializers are worked out.
Steve Naroff87d58b42007-09-16 03:34:24 +0000784 //assert((InitExpr != 0) && "ActOnCompoundLiteral(): missing expression");
Chris Lattner4b009652007-07-25 00:24:17 +0000785 Expr *literalExpr = static_cast<Expr*>(InitExpr);
Anders Carlsson9374b852007-12-05 07:24:19 +0000786
Eli Friedman8c2173d2008-05-20 05:22:08 +0000787 if (literalType->isArrayType()) {
788 if (literalType->getAsVariableArrayType())
789 return Diag(LParenLoc,
790 diag::err_variable_object_no_init,
791 SourceRange(LParenLoc,
792 literalExpr->getSourceRange().getEnd()));
793 } else if (literalType->isIncompleteType()) {
794 return Diag(LParenLoc,
795 diag::err_typecheck_decl_incomplete_type,
796 literalType.getAsString(),
797 SourceRange(LParenLoc,
798 literalExpr->getSourceRange().getEnd()));
799 }
800
Steve Narofff0b23542008-01-10 22:15:12 +0000801 if (CheckInitializerTypes(literalExpr, literalType))
Steve Naroff92590f92008-01-09 20:58:06 +0000802 return true;
Steve Naroffbe37fc02008-01-14 18:19:28 +0000803
Argiris Kirtzidis95256e62008-06-28 06:07:14 +0000804 bool isFileScope = !getCurFunctionDecl() && !getCurMethodDecl();
Steve Naroffbe37fc02008-01-14 18:19:28 +0000805 if (isFileScope) { // 6.5.2.5p3
Steve Narofff0b23542008-01-10 22:15:12 +0000806 if (CheckForConstantInitializer(literalExpr, literalType))
807 return true;
808 }
Steve Naroffbe37fc02008-01-14 18:19:28 +0000809 return new CompoundLiteralExpr(LParenLoc, literalType, literalExpr, isFileScope);
Chris Lattner4b009652007-07-25 00:24:17 +0000810}
811
812Action::ExprResult Sema::
Steve Naroff87d58b42007-09-16 03:34:24 +0000813ActOnInitList(SourceLocation LBraceLoc, ExprTy **initlist, unsigned NumInit,
Anders Carlsson762b7c72007-08-31 04:56:16 +0000814 SourceLocation RBraceLoc) {
Steve Naroffe14e5542007-09-02 02:04:30 +0000815 Expr **InitList = reinterpret_cast<Expr**>(initlist);
Anders Carlsson762b7c72007-08-31 04:56:16 +0000816
Steve Naroff0acc9c92007-09-15 18:49:24 +0000817 // Semantic analysis for initializers is done by ActOnDeclarator() and
Steve Naroff1c9de712007-09-03 01:24:23 +0000818 // CheckInitializer() - it requires knowledge of the object being intialized.
Anders Carlsson762b7c72007-08-31 04:56:16 +0000819
Chris Lattner48d7f382008-04-02 04:24:33 +0000820 InitListExpr *E = new InitListExpr(LBraceLoc, InitList, NumInit, RBraceLoc);
821 E->setType(Context.VoidTy); // FIXME: just a place holder for now.
822 return E;
Chris Lattner4b009652007-07-25 00:24:17 +0000823}
824
Chris Lattnerd1f26b32007-12-20 00:44:32 +0000825bool Sema::CheckVectorCast(SourceRange R, QualType VectorTy, QualType Ty) {
Anders Carlssonf257b4c2007-11-27 05:51:55 +0000826 assert(VectorTy->isVectorType() && "Not a vector type!");
827
828 if (Ty->isVectorType() || Ty->isIntegerType()) {
Chris Lattner8cd0e932008-03-05 18:54:05 +0000829 if (Context.getTypeSize(VectorTy) != Context.getTypeSize(Ty))
Anders Carlssonf257b4c2007-11-27 05:51:55 +0000830 return Diag(R.getBegin(),
831 Ty->isVectorType() ?
832 diag::err_invalid_conversion_between_vectors :
833 diag::err_invalid_conversion_between_vector_and_integer,
834 VectorTy.getAsString().c_str(),
835 Ty.getAsString().c_str(), R);
836 } else
837 return Diag(R.getBegin(),
838 diag::err_invalid_conversion_between_vector_and_scalar,
839 VectorTy.getAsString().c_str(),
840 Ty.getAsString().c_str(), R);
841
842 return false;
843}
844
Chris Lattner4b009652007-07-25 00:24:17 +0000845Action::ExprResult Sema::
Steve Naroff87d58b42007-09-16 03:34:24 +0000846ActOnCastExpr(SourceLocation LParenLoc, TypeTy *Ty,
Chris Lattner4b009652007-07-25 00:24:17 +0000847 SourceLocation RParenLoc, ExprTy *Op) {
Steve Naroff87d58b42007-09-16 03:34:24 +0000848 assert((Ty != 0) && (Op != 0) && "ActOnCastExpr(): missing type or expr");
Chris Lattner4b009652007-07-25 00:24:17 +0000849
850 Expr *castExpr = static_cast<Expr*>(Op);
851 QualType castType = QualType::getFromOpaquePtr(Ty);
852
Steve Naroff68adb482007-08-31 00:32:44 +0000853 UsualUnaryConversions(castExpr);
854
Chris Lattner4b009652007-07-25 00:24:17 +0000855 // C99 6.5.4p2: the cast type needs to be void or scalar and the expression
856 // type needs to be scalar.
Chris Lattnerdb526732007-10-29 04:26:44 +0000857 if (!castType->isVoidType()) { // Cast to void allows any expr type.
Steve Naroff5ad85292008-06-03 12:56:35 +0000858 if (!castType->isScalarType() && !castType->isVectorType()) {
859 // GCC struct/union extension.
860 if (castType == castExpr->getType() &&
Steve Naroff7f1c5b52008-06-03 13:21:30 +0000861 castType->isStructureType() || castType->isUnionType()) {
862 Diag(LParenLoc, diag::ext_typecheck_cast_nonscalar,
863 SourceRange(LParenLoc, RParenLoc));
864 return new CastExpr(castType, castExpr, LParenLoc);
865 } else
Steve Naroff5ad85292008-06-03 12:56:35 +0000866 return Diag(LParenLoc, diag::err_typecheck_cond_expect_scalar,
867 castType.getAsString(), SourceRange(LParenLoc, RParenLoc));
868 }
Steve Narofff459ee52008-01-24 22:55:05 +0000869 if (!castExpr->getType()->isScalarType() &&
870 !castExpr->getType()->isVectorType())
Chris Lattnerdb526732007-10-29 04:26:44 +0000871 return Diag(castExpr->getLocStart(),
872 diag::err_typecheck_expect_scalar_operand,
873 castExpr->getType().getAsString(),castExpr->getSourceRange());
Anders Carlssonf257b4c2007-11-27 05:51:55 +0000874
875 if (castExpr->getType()->isVectorType()) {
876 if (CheckVectorCast(SourceRange(LParenLoc, RParenLoc),
877 castExpr->getType(), castType))
878 return true;
879 } else if (castType->isVectorType()) {
880 if (CheckVectorCast(SourceRange(LParenLoc, RParenLoc),
881 castType, castExpr->getType()))
882 return true;
Chris Lattnerdb526732007-10-29 04:26:44 +0000883 }
Chris Lattner4b009652007-07-25 00:24:17 +0000884 }
885 return new CastExpr(castType, castExpr, LParenLoc);
886}
887
Chris Lattner98a425c2007-11-26 01:40:58 +0000888/// Note that lex is not null here, even if this is the gnu "x ?: y" extension.
889/// In that case, lex = cond.
Chris Lattner4b009652007-07-25 00:24:17 +0000890inline QualType Sema::CheckConditionalOperands( // C99 6.5.15
891 Expr *&cond, Expr *&lex, Expr *&rex, SourceLocation questionLoc) {
892 UsualUnaryConversions(cond);
893 UsualUnaryConversions(lex);
894 UsualUnaryConversions(rex);
895 QualType condT = cond->getType();
896 QualType lexT = lex->getType();
897 QualType rexT = rex->getType();
898
899 // first, check the condition.
900 if (!condT->isScalarType()) { // C99 6.5.15p2
901 Diag(cond->getLocStart(), diag::err_typecheck_cond_expect_scalar,
902 condT.getAsString());
903 return QualType();
904 }
Chris Lattner992ae932008-01-06 22:42:25 +0000905
906 // Now check the two expressions.
907
908 // If both operands have arithmetic type, do the usual arithmetic conversions
909 // to find a common type: C99 6.5.15p3,5.
910 if (lexT->isArithmeticType() && rexT->isArithmeticType()) {
Chris Lattner4b009652007-07-25 00:24:17 +0000911 UsualArithmeticConversions(lex, rex);
912 return lex->getType();
913 }
Chris Lattner992ae932008-01-06 22:42:25 +0000914
915 // If both operands are the same structure or union type, the result is that
916 // type.
Chris Lattner71225142007-07-31 21:27:01 +0000917 if (const RecordType *LHSRT = lexT->getAsRecordType()) { // C99 6.5.15p3
Chris Lattner992ae932008-01-06 22:42:25 +0000918 if (const RecordType *RHSRT = rexT->getAsRecordType())
Chris Lattner98a425c2007-11-26 01:40:58 +0000919 if (LHSRT->getDecl() == RHSRT->getDecl())
Chris Lattner992ae932008-01-06 22:42:25 +0000920 // "If both the operands have structure or union type, the result has
921 // that type." This implies that CV qualifiers are dropped.
922 return lexT.getUnqualifiedType();
Chris Lattner4b009652007-07-25 00:24:17 +0000923 }
Chris Lattner992ae932008-01-06 22:42:25 +0000924
925 // C99 6.5.15p5: "If both operands have void type, the result has void type."
Steve Naroff95cb3892008-05-12 21:44:38 +0000926 // The following || allows only one side to be void (a GCC-ism).
927 if (lexT->isVoidType() || rexT->isVoidType()) {
Eli Friedmanf025aac2008-06-04 19:47:51 +0000928 if (!lexT->isVoidType())
Steve Naroff95cb3892008-05-12 21:44:38 +0000929 Diag(rex->getLocStart(), diag::ext_typecheck_cond_one_void,
930 rex->getSourceRange());
931 if (!rexT->isVoidType())
932 Diag(lex->getLocStart(), diag::ext_typecheck_cond_one_void,
Nuno Lopes4ba41fd2008-06-04 19:14:12 +0000933 lex->getSourceRange());
Eli Friedmanf025aac2008-06-04 19:47:51 +0000934 ImpCastExprToType(lex, Context.VoidTy);
935 ImpCastExprToType(rex, Context.VoidTy);
936 return Context.VoidTy;
Steve Naroff95cb3892008-05-12 21:44:38 +0000937 }
Steve Naroff12ebf272008-01-08 01:11:38 +0000938 // C99 6.5.15p6 - "if one operand is a null pointer constant, the result has
939 // the type of the other operand."
940 if (lexT->isPointerType() && rex->isNullPointerConstant(Context)) {
Chris Lattnere992d6c2008-01-16 19:17:22 +0000941 ImpCastExprToType(rex, lexT); // promote the null to a pointer.
Steve Naroff12ebf272008-01-08 01:11:38 +0000942 return lexT;
943 }
944 if (rexT->isPointerType() && lex->isNullPointerConstant(Context)) {
Chris Lattnere992d6c2008-01-16 19:17:22 +0000945 ImpCastExprToType(lex, rexT); // promote the null to a pointer.
Steve Naroff12ebf272008-01-08 01:11:38 +0000946 return rexT;
947 }
Chris Lattner0ac51632008-01-06 22:50:31 +0000948 // Handle the case where both operands are pointers before we handle null
949 // pointer constants in case both operands are null pointer constants.
Chris Lattner71225142007-07-31 21:27:01 +0000950 if (const PointerType *LHSPT = lexT->getAsPointerType()) { // C99 6.5.15p3,6
951 if (const PointerType *RHSPT = rexT->getAsPointerType()) {
952 // get the "pointed to" types
953 QualType lhptee = LHSPT->getPointeeType();
954 QualType rhptee = RHSPT->getPointeeType();
Chris Lattner4b009652007-07-25 00:24:17 +0000955
Chris Lattner71225142007-07-31 21:27:01 +0000956 // ignore qualifiers on void (C99 6.5.15p3, clause 6)
957 if (lhptee->isVoidType() &&
Chris Lattner9db553e2008-04-02 06:59:01 +0000958 rhptee->isIncompleteOrObjectType()) {
Chris Lattner35fef522008-02-20 20:55:12 +0000959 // Figure out necessary qualifiers (C99 6.5.15p6)
960 QualType destPointee=lhptee.getQualifiedType(rhptee.getCVRQualifiers());
Eli Friedmanca07c902008-02-10 22:59:36 +0000961 QualType destType = Context.getPointerType(destPointee);
962 ImpCastExprToType(lex, destType); // add qualifiers if necessary
963 ImpCastExprToType(rex, destType); // promote to void*
964 return destType;
965 }
Chris Lattner9db553e2008-04-02 06:59:01 +0000966 if (rhptee->isVoidType() && lhptee->isIncompleteOrObjectType()) {
Chris Lattner35fef522008-02-20 20:55:12 +0000967 QualType destPointee=rhptee.getQualifiedType(lhptee.getCVRQualifiers());
Eli Friedmanca07c902008-02-10 22:59:36 +0000968 QualType destType = Context.getPointerType(destPointee);
969 ImpCastExprToType(lex, destType); // add qualifiers if necessary
970 ImpCastExprToType(rex, destType); // promote to void*
971 return destType;
972 }
Chris Lattner4b009652007-07-25 00:24:17 +0000973
Steve Naroff85f0dc52007-10-15 20:41:53 +0000974 if (!Context.typesAreCompatible(lhptee.getUnqualifiedType(),
975 rhptee.getUnqualifiedType())) {
Steve Naroff232324e2008-02-01 22:44:48 +0000976 Diag(questionLoc, diag::warn_typecheck_cond_incompatible_pointers,
Chris Lattner71225142007-07-31 21:27:01 +0000977 lexT.getAsString(), rexT.getAsString(),
978 lex->getSourceRange(), rex->getSourceRange());
Eli Friedman33284862008-01-30 17:02:03 +0000979 // In this situation, we assume void* type. No especially good
980 // reason, but this is what gcc does, and we do have to pick
981 // to get a consistent AST.
982 QualType voidPtrTy = Context.getPointerType(Context.VoidTy);
983 ImpCastExprToType(lex, voidPtrTy);
984 ImpCastExprToType(rex, voidPtrTy);
985 return voidPtrTy;
Chris Lattner71225142007-07-31 21:27:01 +0000986 }
987 // The pointer types are compatible.
Chris Lattner0d9bcea2007-08-30 17:45:32 +0000988 // C99 6.5.15p6: If both operands are pointers to compatible types *or* to
989 // differently qualified versions of compatible types, the result type is
990 // a pointer to an appropriately qualified version of the *composite*
991 // type.
Eli Friedmane38150e2008-05-16 20:37:07 +0000992 // FIXME: Need to calculate the composite type.
Eli Friedmanca07c902008-02-10 22:59:36 +0000993 // FIXME: Need to add qualifiers
Eli Friedmane38150e2008-05-16 20:37:07 +0000994 QualType compositeType = lexT;
995 ImpCastExprToType(lex, compositeType);
996 ImpCastExprToType(rex, compositeType);
997 return compositeType;
Chris Lattner4b009652007-07-25 00:24:17 +0000998 }
Chris Lattner4b009652007-07-25 00:24:17 +0000999 }
Steve Naroff605896f2008-05-31 22:33:45 +00001000 // Need to handle "id<xx>" explicitly. Unlike "id", whose canonical type
1001 // evaluates to "struct objc_object *" (and is handled above when comparing
1002 // id with statically typed objects). FIXME: Do we need an ImpCastExprToType?
1003 if (lexT->isObjCQualifiedIdType() || rexT->isObjCQualifiedIdType()) {
1004 if (ObjCQualifiedIdTypesAreCompatible(lexT, rexT, true))
1005 return Context.getObjCIdType();
1006 }
Chris Lattner992ae932008-01-06 22:42:25 +00001007 // Otherwise, the operands are not compatible.
Chris Lattner4b009652007-07-25 00:24:17 +00001008 Diag(questionLoc, diag::err_typecheck_cond_incompatible_operands,
1009 lexT.getAsString(), rexT.getAsString(),
1010 lex->getSourceRange(), rex->getSourceRange());
1011 return QualType();
1012}
1013
Steve Naroff87d58b42007-09-16 03:34:24 +00001014/// ActOnConditionalOp - Parse a ?: operation. Note that 'LHS' may be null
Chris Lattner4b009652007-07-25 00:24:17 +00001015/// in the case of a the GNU conditional expr extension.
Steve Naroff87d58b42007-09-16 03:34:24 +00001016Action::ExprResult Sema::ActOnConditionalOp(SourceLocation QuestionLoc,
Chris Lattner4b009652007-07-25 00:24:17 +00001017 SourceLocation ColonLoc,
1018 ExprTy *Cond, ExprTy *LHS,
1019 ExprTy *RHS) {
1020 Expr *CondExpr = (Expr *) Cond;
1021 Expr *LHSExpr = (Expr *) LHS, *RHSExpr = (Expr *) RHS;
Chris Lattner98a425c2007-11-26 01:40:58 +00001022
1023 // If this is the gnu "x ?: y" extension, analyze the types as though the LHS
1024 // was the condition.
1025 bool isLHSNull = LHSExpr == 0;
1026 if (isLHSNull)
1027 LHSExpr = CondExpr;
1028
Chris Lattner4b009652007-07-25 00:24:17 +00001029 QualType result = CheckConditionalOperands(CondExpr, LHSExpr,
1030 RHSExpr, QuestionLoc);
1031 if (result.isNull())
1032 return true;
Chris Lattner98a425c2007-11-26 01:40:58 +00001033 return new ConditionalOperator(CondExpr, isLHSNull ? 0 : LHSExpr,
1034 RHSExpr, result);
Chris Lattner4b009652007-07-25 00:24:17 +00001035}
1036
Steve Naroffdb65e052007-08-28 23:30:39 +00001037/// DefaultArgumentPromotion (C99 6.5.2.2p6). Used for function calls that
Steve Naroffbbaed752008-01-29 02:42:22 +00001038/// do not have a prototype. Arguments that have type float are promoted to
1039/// double. All other argument types are converted by UsualUnaryConversions().
Chris Lattner83bd5eb2007-12-28 05:29:59 +00001040void Sema::DefaultArgumentPromotion(Expr *&Expr) {
1041 QualType Ty = Expr->getType();
1042 assert(!Ty.isNull() && "DefaultArgumentPromotion - missing type");
Steve Naroffdb65e052007-08-28 23:30:39 +00001043
Chris Lattner11216032008-06-27 22:48:56 +00001044 // If this is a 'float' (CVR qualified or typedef) promote to double.
1045 if (const BuiltinType *BT = Ty->getAsBuiltinType())
1046 if (BT->getKind() == BuiltinType::Float)
1047 return ImpCastExprToType(Expr, Context.DoubleTy);
1048
1049 UsualUnaryConversions(Expr);
Steve Naroffdb65e052007-08-28 23:30:39 +00001050}
1051
Chris Lattner4b009652007-07-25 00:24:17 +00001052/// DefaultFunctionArrayConversion (C99 6.3.2.1p3, C99 6.3.2.1p4).
Chris Lattner48d7f382008-04-02 04:24:33 +00001053void Sema::DefaultFunctionArrayConversion(Expr *&E) {
1054 QualType Ty = E->getType();
1055 assert(!Ty.isNull() && "DefaultFunctionArrayConversion - missing type");
Chris Lattner4b009652007-07-25 00:24:17 +00001056
Chris Lattner48d7f382008-04-02 04:24:33 +00001057 if (const ReferenceType *ref = Ty->getAsReferenceType()) {
Chris Lattnera05f7d22008-04-02 17:45:06 +00001058 ImpCastExprToType(E, ref->getPointeeType()); // C++ [expr]
Chris Lattner48d7f382008-04-02 04:24:33 +00001059 Ty = E->getType();
Chris Lattner4b009652007-07-25 00:24:17 +00001060 }
Chris Lattner48d7f382008-04-02 04:24:33 +00001061 if (Ty->isFunctionType())
1062 ImpCastExprToType(E, Context.getPointerType(Ty));
Chris Lattner19eb97e2008-04-02 05:18:44 +00001063 else if (Ty->isArrayType())
1064 ImpCastExprToType(E, Context.getArrayDecayedType(Ty));
Chris Lattner4b009652007-07-25 00:24:17 +00001065}
1066
Nate Begeman9f3bfb72008-01-17 17:46:27 +00001067/// UsualUnaryConversions - Performs various conversions that are common to most
Chris Lattner4b009652007-07-25 00:24:17 +00001068/// operators (C99 6.3). The conversions of array and function types are
1069/// sometimes surpressed. For example, the array->pointer conversion doesn't
1070/// apply if the array is an argument to the sizeof or address (&) operators.
1071/// In these instances, this routine should *not* be called.
Chris Lattner83bd5eb2007-12-28 05:29:59 +00001072Expr *Sema::UsualUnaryConversions(Expr *&Expr) {
1073 QualType Ty = Expr->getType();
1074 assert(!Ty.isNull() && "UsualUnaryConversions - missing type");
Chris Lattner4b009652007-07-25 00:24:17 +00001075
Chris Lattner83bd5eb2007-12-28 05:29:59 +00001076 if (const ReferenceType *Ref = Ty->getAsReferenceType()) {
Chris Lattnera05f7d22008-04-02 17:45:06 +00001077 ImpCastExprToType(Expr, Ref->getPointeeType()); // C++ [expr]
Chris Lattner83bd5eb2007-12-28 05:29:59 +00001078 Ty = Expr->getType();
Chris Lattner4b009652007-07-25 00:24:17 +00001079 }
Chris Lattner83bd5eb2007-12-28 05:29:59 +00001080 if (Ty->isPromotableIntegerType()) // C99 6.3.1.1p2
Chris Lattnere992d6c2008-01-16 19:17:22 +00001081 ImpCastExprToType(Expr, Context.IntTy);
Chris Lattner4b009652007-07-25 00:24:17 +00001082 else
Chris Lattner83bd5eb2007-12-28 05:29:59 +00001083 DefaultFunctionArrayConversion(Expr);
1084
1085 return Expr;
Chris Lattner4b009652007-07-25 00:24:17 +00001086}
1087
Chris Lattner0d9bcea2007-08-30 17:45:32 +00001088/// UsualArithmeticConversions - Performs various conversions that are common to
Chris Lattner4b009652007-07-25 00:24:17 +00001089/// binary operators (C99 6.3.1.8). If both operands aren't arithmetic, this
1090/// routine returns the first non-arithmetic type found. The client is
1091/// responsible for emitting appropriate error diagnostics.
Chris Lattner48d7f382008-04-02 04:24:33 +00001092/// FIXME: verify the conversion rules for "complex int" are consistent with
1093/// GCC.
Steve Naroff8f708362007-08-24 19:07:16 +00001094QualType Sema::UsualArithmeticConversions(Expr *&lhsExpr, Expr *&rhsExpr,
1095 bool isCompAssign) {
Steve Naroffb2f9f552007-08-25 19:54:59 +00001096 if (!isCompAssign) {
1097 UsualUnaryConversions(lhsExpr);
1098 UsualUnaryConversions(rhsExpr);
1099 }
Steve Naroff7438fdf2007-10-18 18:55:53 +00001100 // For conversion purposes, we ignore any qualifiers.
1101 // For example, "const float" and "float" are equivalent.
Steve Naroff1ddb6f52007-11-10 19:45:54 +00001102 QualType lhs = lhsExpr->getType().getCanonicalType().getUnqualifiedType();
1103 QualType rhs = rhsExpr->getType().getCanonicalType().getUnqualifiedType();
Chris Lattner4b009652007-07-25 00:24:17 +00001104
1105 // If both types are identical, no conversion is needed.
Steve Naroff7438fdf2007-10-18 18:55:53 +00001106 if (lhs == rhs)
1107 return lhs;
Chris Lattner4b009652007-07-25 00:24:17 +00001108
1109 // If either side is a non-arithmetic type (e.g. a pointer), we are done.
1110 // The caller can deal with this (e.g. pointer + int).
1111 if (!lhs->isArithmeticType() || !rhs->isArithmeticType())
Steve Naroff8f708362007-08-24 19:07:16 +00001112 return lhs;
Chris Lattner4b009652007-07-25 00:24:17 +00001113
1114 // At this point, we have two different arithmetic types.
1115
1116 // Handle complex types first (C99 6.3.1.8p1).
1117 if (lhs->isComplexType() || rhs->isComplexType()) {
Steve Naroff43001212008-01-15 19:36:10 +00001118 // if we have an integer operand, the result is the complex type.
Steve Naroffe8419ca2008-01-15 22:21:49 +00001119 if (rhs->isIntegerType() || rhs->isComplexIntegerType()) {
Eli Friedman50727042008-02-08 01:19:44 +00001120 // convert the rhs to the lhs complex type.
Chris Lattnere992d6c2008-01-16 19:17:22 +00001121 if (!isCompAssign) ImpCastExprToType(rhsExpr, lhs);
Steve Naroff8f708362007-08-24 19:07:16 +00001122 return lhs;
Steve Naroff43001212008-01-15 19:36:10 +00001123 }
Steve Naroffe8419ca2008-01-15 22:21:49 +00001124 if (lhs->isIntegerType() || lhs->isComplexIntegerType()) {
Eli Friedman50727042008-02-08 01:19:44 +00001125 // convert the lhs to the rhs complex type.
Chris Lattnere992d6c2008-01-16 19:17:22 +00001126 if (!isCompAssign) ImpCastExprToType(lhsExpr, rhs);
Steve Naroff8f708362007-08-24 19:07:16 +00001127 return rhs;
Chris Lattner4b009652007-07-25 00:24:17 +00001128 }
Steve Naroff3cf497f2007-08-27 01:27:54 +00001129 // This handles complex/complex, complex/float, or float/complex.
1130 // When both operands are complex, the shorter operand is converted to the
1131 // type of the longer, and that is the type of the result. This corresponds
1132 // to what is done when combining two real floating-point operands.
1133 // The fun begins when size promotion occur across type domains.
1134 // From H&S 6.3.4: When one operand is complex and the other is a real
1135 // floating-point type, the less precise type is converted, within it's
1136 // real or complex domain, to the precision of the other type. For example,
1137 // when combining a "long double" with a "double _Complex", the
1138 // "double _Complex" is promoted to "long double _Complex".
Chris Lattnerd7135b42008-04-06 23:38:49 +00001139 int result = Context.getFloatingTypeOrder(lhs, rhs);
Steve Naroff45fc9822007-08-27 15:30:22 +00001140
1141 if (result > 0) { // The left side is bigger, convert rhs.
Steve Naroff3b565d62007-08-27 21:32:55 +00001142 rhs = Context.getFloatingTypeOfSizeWithinDomain(lhs, rhs);
1143 if (!isCompAssign)
Chris Lattnere992d6c2008-01-16 19:17:22 +00001144 ImpCastExprToType(rhsExpr, rhs);
Steve Naroff3b565d62007-08-27 21:32:55 +00001145 } else if (result < 0) { // The right side is bigger, convert lhs.
1146 lhs = Context.getFloatingTypeOfSizeWithinDomain(rhs, lhs);
1147 if (!isCompAssign)
Chris Lattnere992d6c2008-01-16 19:17:22 +00001148 ImpCastExprToType(lhsExpr, lhs);
Steve Naroff3b565d62007-08-27 21:32:55 +00001149 }
1150 // At this point, lhs and rhs have the same rank/size. Now, make sure the
1151 // domains match. This is a requirement for our implementation, C99
1152 // does not require this promotion.
1153 if (lhs != rhs) { // Domains don't match, we have complex/float mix.
1154 if (lhs->isRealFloatingType()) { // handle "double, _Complex double".
Steve Naroff3b6157f2007-08-27 21:43:43 +00001155 if (!isCompAssign)
Chris Lattnere992d6c2008-01-16 19:17:22 +00001156 ImpCastExprToType(lhsExpr, rhs);
Steve Naroff3b6157f2007-08-27 21:43:43 +00001157 return rhs;
Steve Naroff3b565d62007-08-27 21:32:55 +00001158 } else { // handle "_Complex double, double".
Steve Naroff3b6157f2007-08-27 21:43:43 +00001159 if (!isCompAssign)
Chris Lattnere992d6c2008-01-16 19:17:22 +00001160 ImpCastExprToType(rhsExpr, lhs);
Steve Naroff3b6157f2007-08-27 21:43:43 +00001161 return lhs;
Steve Naroff3b565d62007-08-27 21:32:55 +00001162 }
Chris Lattner4b009652007-07-25 00:24:17 +00001163 }
Steve Naroff3b6157f2007-08-27 21:43:43 +00001164 return lhs; // The domain/size match exactly.
Chris Lattner4b009652007-07-25 00:24:17 +00001165 }
1166 // Now handle "real" floating types (i.e. float, double, long double).
1167 if (lhs->isRealFloatingType() || rhs->isRealFloatingType()) {
1168 // if we have an integer operand, the result is the real floating type.
Steve Naroffe8419ca2008-01-15 22:21:49 +00001169 if (rhs->isIntegerType() || rhs->isComplexIntegerType()) {
Eli Friedman50727042008-02-08 01:19:44 +00001170 // convert rhs to the lhs floating point type.
Chris Lattnere992d6c2008-01-16 19:17:22 +00001171 if (!isCompAssign) ImpCastExprToType(rhsExpr, lhs);
Steve Naroff8f708362007-08-24 19:07:16 +00001172 return lhs;
Chris Lattner4b009652007-07-25 00:24:17 +00001173 }
Steve Naroffe8419ca2008-01-15 22:21:49 +00001174 if (lhs->isIntegerType() || lhs->isComplexIntegerType()) {
Eli Friedman50727042008-02-08 01:19:44 +00001175 // convert lhs to the rhs floating point type.
Chris Lattnere992d6c2008-01-16 19:17:22 +00001176 if (!isCompAssign) ImpCastExprToType(lhsExpr, rhs);
Steve Naroff8f708362007-08-24 19:07:16 +00001177 return rhs;
Chris Lattner4b009652007-07-25 00:24:17 +00001178 }
1179 // We have two real floating types, float/complex combos were handled above.
1180 // Convert the smaller operand to the bigger result.
Chris Lattnerd7135b42008-04-06 23:38:49 +00001181 int result = Context.getFloatingTypeOrder(lhs, rhs);
Steve Naroff45fc9822007-08-27 15:30:22 +00001182
1183 if (result > 0) { // convert the rhs
Chris Lattnere992d6c2008-01-16 19:17:22 +00001184 if (!isCompAssign) ImpCastExprToType(rhsExpr, lhs);
Steve Naroff8f708362007-08-24 19:07:16 +00001185 return lhs;
Chris Lattner4b009652007-07-25 00:24:17 +00001186 }
Steve Naroff45fc9822007-08-27 15:30:22 +00001187 if (result < 0) { // convert the lhs
Chris Lattnere992d6c2008-01-16 19:17:22 +00001188 if (!isCompAssign) ImpCastExprToType(lhsExpr, rhs); // convert the lhs
Steve Naroff45fc9822007-08-27 15:30:22 +00001189 return rhs;
1190 }
1191 assert(0 && "Sema::UsualArithmeticConversions(): illegal float comparison");
Chris Lattner4b009652007-07-25 00:24:17 +00001192 }
Steve Naroff43001212008-01-15 19:36:10 +00001193 if (lhs->isComplexIntegerType() || rhs->isComplexIntegerType()) {
1194 // Handle GCC complex int extension.
Steve Naroff43001212008-01-15 19:36:10 +00001195 const ComplexType *lhsComplexInt = lhs->getAsComplexIntegerType();
Eli Friedman50727042008-02-08 01:19:44 +00001196 const ComplexType *rhsComplexInt = rhs->getAsComplexIntegerType();
Steve Naroff43001212008-01-15 19:36:10 +00001197
Eli Friedman50727042008-02-08 01:19:44 +00001198 if (lhsComplexInt && rhsComplexInt) {
Chris Lattner51285d82008-04-06 23:55:33 +00001199 if (Context.getIntegerTypeOrder(lhsComplexInt->getElementType(),
1200 rhsComplexInt->getElementType()) >= 0) {
Eli Friedman94075c02008-02-08 01:24:30 +00001201 // convert the rhs
1202 if (!isCompAssign) ImpCastExprToType(rhsExpr, lhs);
1203 return lhs;
Eli Friedman50727042008-02-08 01:19:44 +00001204 }
1205 if (!isCompAssign)
Eli Friedman94075c02008-02-08 01:24:30 +00001206 ImpCastExprToType(lhsExpr, rhs); // convert the lhs
Eli Friedman50727042008-02-08 01:19:44 +00001207 return rhs;
1208 } else if (lhsComplexInt && rhs->isIntegerType()) {
1209 // convert the rhs to the lhs complex type.
1210 if (!isCompAssign) ImpCastExprToType(rhsExpr, lhs);
1211 return lhs;
1212 } else if (rhsComplexInt && lhs->isIntegerType()) {
1213 // convert the lhs to the rhs complex type.
1214 if (!isCompAssign) ImpCastExprToType(lhsExpr, rhs);
1215 return rhs;
1216 }
Steve Naroff43001212008-01-15 19:36:10 +00001217 }
Chris Lattner4b009652007-07-25 00:24:17 +00001218 // Finally, we have two differing integer types.
Eli Friedman0832dbc2008-06-28 06:23:08 +00001219 // The rules for this case are in C99 6.3.1.8
1220 int compare = Context.getIntegerTypeOrder(lhs, rhs);
1221 bool lhsSigned = lhs->isSignedIntegerType(),
1222 rhsSigned = rhs->isSignedIntegerType();
1223 QualType destType;
1224 if (lhsSigned == rhsSigned) {
1225 // Same signedness; use the higher-ranked type
1226 destType = compare >= 0 ? lhs : rhs;
1227 } else if (compare != (lhsSigned ? 1 : -1)) {
1228 // The unsigned type has greater than or equal rank to the
1229 // signed type, so use the unsigned type
1230 destType = lhsSigned ? rhs : lhs;
1231 } else if (Context.getIntWidth(lhs) != Context.getIntWidth(rhs)) {
1232 // The two types are different widths; if we are here, that
1233 // means the signed type is larger than the unsigned type, so
1234 // use the signed type.
1235 destType = lhsSigned ? lhs : rhs;
1236 } else {
1237 // The signed type is higher-ranked than the unsigned type,
1238 // but isn't actually any bigger (like unsigned int and long
1239 // on most 32-bit systems). Use the unsigned type corresponding
1240 // to the signed type.
1241 destType = Context.getCorrespondingUnsignedType(lhsSigned ? lhs : rhs);
Chris Lattner4b009652007-07-25 00:24:17 +00001242 }
Eli Friedman0832dbc2008-06-28 06:23:08 +00001243 if (!isCompAssign) {
1244 ImpCastExprToType(lhsExpr, destType);
1245 ImpCastExprToType(rhsExpr, destType);
1246 }
1247 return destType;
Chris Lattner4b009652007-07-25 00:24:17 +00001248}
1249
1250// CheckPointerTypesForAssignment - This is a very tricky routine (despite
1251// being closely modeled after the C99 spec:-). The odd characteristic of this
1252// routine is it effectively iqnores the qualifiers on the top level pointee.
1253// This circumvents the usual type rules specified in 6.2.7p1 & 6.7.5.[1-3].
1254// FIXME: add a couple examples in this comment.
Chris Lattner005ed752008-01-04 18:04:52 +00001255Sema::AssignConvertType
Chris Lattner4b009652007-07-25 00:24:17 +00001256Sema::CheckPointerTypesForAssignment(QualType lhsType, QualType rhsType) {
1257 QualType lhptee, rhptee;
1258
1259 // get the "pointed to" type (ignoring qualifiers at the top level)
Chris Lattner71225142007-07-31 21:27:01 +00001260 lhptee = lhsType->getAsPointerType()->getPointeeType();
1261 rhptee = rhsType->getAsPointerType()->getPointeeType();
Chris Lattner4b009652007-07-25 00:24:17 +00001262
1263 // make sure we operate on the canonical type
1264 lhptee = lhptee.getCanonicalType();
1265 rhptee = rhptee.getCanonicalType();
1266
Chris Lattner005ed752008-01-04 18:04:52 +00001267 AssignConvertType ConvTy = Compatible;
Chris Lattner4b009652007-07-25 00:24:17 +00001268
1269 // C99 6.5.16.1p1: This following citation is common to constraints
1270 // 3 & 4 (below). ...and the type *pointed to* by the left has all the
1271 // qualifiers of the type *pointed to* by the right;
Chris Lattner35fef522008-02-20 20:55:12 +00001272 // FIXME: Handle ASQualType
1273 if ((lhptee.getCVRQualifiers() & rhptee.getCVRQualifiers()) !=
1274 rhptee.getCVRQualifiers())
Chris Lattner005ed752008-01-04 18:04:52 +00001275 ConvTy = CompatiblePointerDiscardsQualifiers;
Chris Lattner4b009652007-07-25 00:24:17 +00001276
1277 // C99 6.5.16.1p1 (constraint 4): If one operand is a pointer to an object or
1278 // incomplete type and the other is a pointer to a qualified or unqualified
1279 // version of void...
Chris Lattner4ca3d772008-01-03 22:56:36 +00001280 if (lhptee->isVoidType()) {
Chris Lattner9db553e2008-04-02 06:59:01 +00001281 if (rhptee->isIncompleteOrObjectType())
Chris Lattner005ed752008-01-04 18:04:52 +00001282 return ConvTy;
Chris Lattner4ca3d772008-01-03 22:56:36 +00001283
1284 // As an extension, we allow cast to/from void* to function pointer.
Chris Lattner9db553e2008-04-02 06:59:01 +00001285 assert(rhptee->isFunctionType());
1286 return FunctionVoidPointer;
Chris Lattner4ca3d772008-01-03 22:56:36 +00001287 }
1288
1289 if (rhptee->isVoidType()) {
Chris Lattner9db553e2008-04-02 06:59:01 +00001290 if (lhptee->isIncompleteOrObjectType())
Chris Lattner005ed752008-01-04 18:04:52 +00001291 return ConvTy;
Chris Lattner4ca3d772008-01-03 22:56:36 +00001292
1293 // As an extension, we allow cast to/from void* to function pointer.
Chris Lattner9db553e2008-04-02 06:59:01 +00001294 assert(lhptee->isFunctionType());
1295 return FunctionVoidPointer;
Chris Lattner4ca3d772008-01-03 22:56:36 +00001296 }
1297
Chris Lattner4b009652007-07-25 00:24:17 +00001298 // C99 6.5.16.1p1 (constraint 3): both operands are pointers to qualified or
1299 // unqualified versions of compatible types, ...
Chris Lattner4ca3d772008-01-03 22:56:36 +00001300 if (!Context.typesAreCompatible(lhptee.getUnqualifiedType(),
1301 rhptee.getUnqualifiedType()))
1302 return IncompatiblePointer; // this "trumps" PointerAssignDiscardsQualifiers
Chris Lattner005ed752008-01-04 18:04:52 +00001303 return ConvTy;
Chris Lattner4b009652007-07-25 00:24:17 +00001304}
1305
1306/// CheckAssignmentConstraints (C99 6.5.16) - This routine currently
1307/// has code to accommodate several GCC extensions when type checking
1308/// pointers. Here are some objectionable examples that GCC considers warnings:
1309///
1310/// int a, *pint;
1311/// short *pshort;
1312/// struct foo *pfoo;
1313///
1314/// pint = pshort; // warning: assignment from incompatible pointer type
1315/// a = pint; // warning: assignment makes integer from pointer without a cast
1316/// pint = a; // warning: assignment makes pointer from integer without a cast
1317/// pint = pfoo; // warning: assignment from incompatible pointer type
1318///
1319/// As a result, the code for dealing with pointers is more complex than the
1320/// C99 spec dictates.
Chris Lattner4b009652007-07-25 00:24:17 +00001321///
Chris Lattner005ed752008-01-04 18:04:52 +00001322Sema::AssignConvertType
Chris Lattner4b009652007-07-25 00:24:17 +00001323Sema::CheckAssignmentConstraints(QualType lhsType, QualType rhsType) {
Chris Lattner1853da22008-01-04 23:18:45 +00001324 // Get canonical types. We're not formatting these types, just comparing
1325 // them.
Eli Friedman48d0bb02008-05-30 18:07:22 +00001326 lhsType = lhsType.getCanonicalType().getUnqualifiedType();
1327 rhsType = rhsType.getCanonicalType().getUnqualifiedType();
1328
1329 if (lhsType == rhsType)
Chris Lattnerfdd96d72008-01-07 17:51:46 +00001330 return Compatible; // Common case: fast path an exact match.
Chris Lattner4b009652007-07-25 00:24:17 +00001331
Anders Carlssoncebb8d62007-10-12 23:56:29 +00001332 if (lhsType->isReferenceType() || rhsType->isReferenceType()) {
Chris Lattnere1577e22008-04-07 06:52:53 +00001333 if (Context.typesAreCompatible(lhsType, rhsType))
Anders Carlssoncebb8d62007-10-12 23:56:29 +00001334 return Compatible;
Chris Lattner1853da22008-01-04 23:18:45 +00001335 return Incompatible;
Fariborz Jahanian957442d2007-12-19 17:45:58 +00001336 }
Eli Friedman48d0bb02008-05-30 18:07:22 +00001337
Chris Lattnerfe1f4032008-04-07 05:30:13 +00001338 if (lhsType->isObjCQualifiedIdType() || rhsType->isObjCQualifiedIdType()) {
1339 if (ObjCQualifiedIdTypesAreCompatible(lhsType, rhsType, false))
Fariborz Jahanian957442d2007-12-19 17:45:58 +00001340 return Compatible;
Steve Naroff936c4362008-06-03 14:04:54 +00001341 // Relax integer conversions like we do for pointers below.
1342 if (rhsType->isIntegerType())
1343 return IntToPointer;
1344 if (lhsType->isIntegerType())
1345 return PointerToInt;
Chris Lattner1853da22008-01-04 23:18:45 +00001346 return Incompatible;
Fariborz Jahanian957442d2007-12-19 17:45:58 +00001347 }
Chris Lattnerdb22bf42008-01-04 23:32:24 +00001348
Chris Lattner390564e2008-04-07 06:49:41 +00001349 if (isa<VectorType>(lhsType) || isa<VectorType>(rhsType)) {
Nate Begemanaf6ed502008-04-18 23:10:10 +00001350 // For ExtVector, allow vector splats; float -> <n x float>
1351 if (const ExtVectorType *LV = dyn_cast<ExtVectorType>(lhsType)) {
Chris Lattnerdb22bf42008-01-04 23:32:24 +00001352 if (LV->getElementType().getTypePtr() == rhsType.getTypePtr())
1353 return Compatible;
1354 }
Eli Friedman48d0bb02008-05-30 18:07:22 +00001355
Chris Lattnerdb22bf42008-01-04 23:32:24 +00001356 // If LHS and RHS are both vectors of integer or both vectors of floating
1357 // point types, and the total vector length is the same, allow the
1358 // conversion. This is a bitcast; no bits are changed but the result type
1359 // is different.
1360 if (getLangOptions().LaxVectorConversions &&
1361 lhsType->isVectorType() && rhsType->isVectorType()) {
1362 if ((lhsType->isIntegerType() && rhsType->isIntegerType()) ||
1363 (lhsType->isRealFloatingType() && rhsType->isRealFloatingType())) {
Chris Lattner8cd0e932008-03-05 18:54:05 +00001364 if (Context.getTypeSize(lhsType) == Context.getTypeSize(rhsType))
Nate Begemanec2d1062007-12-30 02:59:45 +00001365 return Compatible;
1366 }
Chris Lattnerdb22bf42008-01-04 23:32:24 +00001367 }
1368 return Incompatible;
1369 }
Eli Friedman48d0bb02008-05-30 18:07:22 +00001370
Chris Lattnerdb22bf42008-01-04 23:32:24 +00001371 if (lhsType->isArithmeticType() && rhsType->isArithmeticType())
Chris Lattner4b009652007-07-25 00:24:17 +00001372 return Compatible;
Eli Friedman48d0bb02008-05-30 18:07:22 +00001373
Chris Lattner390564e2008-04-07 06:49:41 +00001374 if (isa<PointerType>(lhsType)) {
Chris Lattner4b009652007-07-25 00:24:17 +00001375 if (rhsType->isIntegerType())
Chris Lattnerd951b7b2008-01-04 18:22:42 +00001376 return IntToPointer;
Eli Friedman48d0bb02008-05-30 18:07:22 +00001377
Chris Lattner390564e2008-04-07 06:49:41 +00001378 if (isa<PointerType>(rhsType))
Chris Lattner4b009652007-07-25 00:24:17 +00001379 return CheckPointerTypesForAssignment(lhsType, rhsType);
Chris Lattner1853da22008-01-04 23:18:45 +00001380 return Incompatible;
1381 }
1382
Chris Lattner390564e2008-04-07 06:49:41 +00001383 if (isa<PointerType>(rhsType)) {
Chris Lattner4b009652007-07-25 00:24:17 +00001384 // C99 6.5.16.1p1: the left operand is _Bool and the right is a pointer.
Eli Friedman48d0bb02008-05-30 18:07:22 +00001385 if (lhsType == Context.BoolTy)
1386 return Compatible;
1387
1388 if (lhsType->isIntegerType())
Chris Lattnerd951b7b2008-01-04 18:22:42 +00001389 return PointerToInt;
Chris Lattner4b009652007-07-25 00:24:17 +00001390
Chris Lattner390564e2008-04-07 06:49:41 +00001391 if (isa<PointerType>(lhsType))
Chris Lattner4b009652007-07-25 00:24:17 +00001392 return CheckPointerTypesForAssignment(lhsType, rhsType);
Chris Lattner1853da22008-01-04 23:18:45 +00001393 return Incompatible;
Chris Lattner1853da22008-01-04 23:18:45 +00001394 }
Eli Friedman48d0bb02008-05-30 18:07:22 +00001395
Chris Lattner1853da22008-01-04 23:18:45 +00001396 if (isa<TagType>(lhsType) && isa<TagType>(rhsType)) {
Chris Lattner390564e2008-04-07 06:49:41 +00001397 if (Context.typesAreCompatible(lhsType, rhsType))
Chris Lattner4b009652007-07-25 00:24:17 +00001398 return Compatible;
Chris Lattner4b009652007-07-25 00:24:17 +00001399 }
1400 return Incompatible;
1401}
1402
Chris Lattner005ed752008-01-04 18:04:52 +00001403Sema::AssignConvertType
Chris Lattner4b009652007-07-25 00:24:17 +00001404Sema::CheckSingleAssignmentConstraints(QualType lhsType, Expr *&rExpr) {
Steve Naroffcdee22d2007-11-27 17:58:44 +00001405 // C99 6.5.16.1p1: the left operand is a pointer and the right is
1406 // a null pointer constant.
Ted Kremenek42730c52008-01-07 19:49:32 +00001407 if ((lhsType->isPointerType() || lhsType->isObjCQualifiedIdType())
Fariborz Jahaniana13effb2008-01-03 18:46:52 +00001408 && rExpr->isNullPointerConstant(Context)) {
Chris Lattnere992d6c2008-01-16 19:17:22 +00001409 ImpCastExprToType(rExpr, lhsType);
Steve Naroffcdee22d2007-11-27 17:58:44 +00001410 return Compatible;
1411 }
Chris Lattner5f505bf2007-10-16 02:55:40 +00001412 // This check seems unnatural, however it is necessary to ensure the proper
Chris Lattner4b009652007-07-25 00:24:17 +00001413 // conversion of functions/arrays. If the conversion were done for all
Steve Naroff0acc9c92007-09-15 18:49:24 +00001414 // DeclExpr's (created by ActOnIdentifierExpr), it would mess up the unary
Chris Lattner4b009652007-07-25 00:24:17 +00001415 // expressions that surpress this implicit conversion (&, sizeof).
Chris Lattner5f505bf2007-10-16 02:55:40 +00001416 //
1417 // Suppress this for references: C99 8.5.3p5. FIXME: revisit when references
1418 // are better understood.
1419 if (!lhsType->isReferenceType())
1420 DefaultFunctionArrayConversion(rExpr);
Steve Naroff0f32f432007-08-24 22:33:52 +00001421
Chris Lattner005ed752008-01-04 18:04:52 +00001422 Sema::AssignConvertType result =
1423 CheckAssignmentConstraints(lhsType, rExpr->getType());
Steve Naroff0f32f432007-08-24 22:33:52 +00001424
1425 // C99 6.5.16.1p2: The value of the right operand is converted to the
1426 // type of the assignment expression.
1427 if (rExpr->getType() != lhsType)
Chris Lattnere992d6c2008-01-16 19:17:22 +00001428 ImpCastExprToType(rExpr, lhsType);
Steve Naroff0f32f432007-08-24 22:33:52 +00001429 return result;
Chris Lattner4b009652007-07-25 00:24:17 +00001430}
1431
Chris Lattner005ed752008-01-04 18:04:52 +00001432Sema::AssignConvertType
Chris Lattner4b009652007-07-25 00:24:17 +00001433Sema::CheckCompoundAssignmentConstraints(QualType lhsType, QualType rhsType) {
1434 return CheckAssignmentConstraints(lhsType, rhsType);
1435}
1436
Chris Lattner2c8bff72007-12-12 05:47:28 +00001437QualType Sema::InvalidOperands(SourceLocation loc, Expr *&lex, Expr *&rex) {
Chris Lattner4b009652007-07-25 00:24:17 +00001438 Diag(loc, diag::err_typecheck_invalid_operands,
1439 lex->getType().getAsString(), rex->getType().getAsString(),
1440 lex->getSourceRange(), rex->getSourceRange());
Chris Lattner2c8bff72007-12-12 05:47:28 +00001441 return QualType();
Chris Lattner4b009652007-07-25 00:24:17 +00001442}
1443
1444inline QualType Sema::CheckVectorOperands(SourceLocation loc, Expr *&lex,
1445 Expr *&rex) {
Nate Begeman03105572008-04-04 01:30:25 +00001446 // For conversion purposes, we ignore any qualifiers.
1447 // For example, "const float" and "float" are equivalent.
1448 QualType lhsType = lex->getType().getCanonicalType().getUnqualifiedType();
1449 QualType rhsType = rex->getType().getCanonicalType().getUnqualifiedType();
Chris Lattner4b009652007-07-25 00:24:17 +00001450
1451 // make sure the vector types are identical.
Nate Begeman03105572008-04-04 01:30:25 +00001452 if (lhsType == rhsType)
Chris Lattner4b009652007-07-25 00:24:17 +00001453 return lhsType;
Nate Begemanec2d1062007-12-30 02:59:45 +00001454
Nate Begemanaf6ed502008-04-18 23:10:10 +00001455 // if the lhs is an extended vector and the rhs is a scalar of the same type,
Nate Begemanec2d1062007-12-30 02:59:45 +00001456 // promote the rhs to the vector type.
Nate Begemanaf6ed502008-04-18 23:10:10 +00001457 if (const ExtVectorType *V = lhsType->getAsExtVectorType()) {
Nate Begemanec2d1062007-12-30 02:59:45 +00001458 if (V->getElementType().getCanonicalType().getTypePtr()
1459 == rhsType.getCanonicalType().getTypePtr()) {
Chris Lattnere992d6c2008-01-16 19:17:22 +00001460 ImpCastExprToType(rex, lhsType);
Nate Begemanec2d1062007-12-30 02:59:45 +00001461 return lhsType;
1462 }
1463 }
1464
Nate Begemanaf6ed502008-04-18 23:10:10 +00001465 // if the rhs is an extended vector and the lhs is a scalar of the same type,
Nate Begemanec2d1062007-12-30 02:59:45 +00001466 // promote the lhs to the vector type.
Nate Begemanaf6ed502008-04-18 23:10:10 +00001467 if (const ExtVectorType *V = rhsType->getAsExtVectorType()) {
Nate Begemanec2d1062007-12-30 02:59:45 +00001468 if (V->getElementType().getCanonicalType().getTypePtr()
1469 == lhsType.getCanonicalType().getTypePtr()) {
Chris Lattnere992d6c2008-01-16 19:17:22 +00001470 ImpCastExprToType(lex, rhsType);
Nate Begemanec2d1062007-12-30 02:59:45 +00001471 return rhsType;
1472 }
1473 }
1474
Chris Lattner4b009652007-07-25 00:24:17 +00001475 // You cannot convert between vector values of different size.
1476 Diag(loc, diag::err_typecheck_vector_not_convertable,
1477 lex->getType().getAsString(), rex->getType().getAsString(),
1478 lex->getSourceRange(), rex->getSourceRange());
1479 return QualType();
1480}
1481
1482inline QualType Sema::CheckMultiplyDivideOperands(
Steve Naroff8f708362007-08-24 19:07:16 +00001483 Expr *&lex, Expr *&rex, SourceLocation loc, bool isCompAssign)
Chris Lattner4b009652007-07-25 00:24:17 +00001484{
1485 QualType lhsType = lex->getType(), rhsType = rex->getType();
1486
1487 if (lhsType->isVectorType() || rhsType->isVectorType())
1488 return CheckVectorOperands(loc, lex, rex);
1489
Steve Naroff8f708362007-08-24 19:07:16 +00001490 QualType compType = UsualArithmeticConversions(lex, rex, isCompAssign);
Chris Lattner4b009652007-07-25 00:24:17 +00001491
Chris Lattner4b009652007-07-25 00:24:17 +00001492 if (lex->getType()->isArithmeticType() && rex->getType()->isArithmeticType())
Steve Naroff8f708362007-08-24 19:07:16 +00001493 return compType;
Chris Lattner2c8bff72007-12-12 05:47:28 +00001494 return InvalidOperands(loc, lex, rex);
Chris Lattner4b009652007-07-25 00:24:17 +00001495}
1496
1497inline QualType Sema::CheckRemainderOperands(
Steve Naroff8f708362007-08-24 19:07:16 +00001498 Expr *&lex, Expr *&rex, SourceLocation loc, bool isCompAssign)
Chris Lattner4b009652007-07-25 00:24:17 +00001499{
1500 QualType lhsType = lex->getType(), rhsType = rex->getType();
1501
Steve Naroff8f708362007-08-24 19:07:16 +00001502 QualType compType = UsualArithmeticConversions(lex, rex, isCompAssign);
Chris Lattner4b009652007-07-25 00:24:17 +00001503
Chris Lattner4b009652007-07-25 00:24:17 +00001504 if (lex->getType()->isIntegerType() && rex->getType()->isIntegerType())
Steve Naroff8f708362007-08-24 19:07:16 +00001505 return compType;
Chris Lattner2c8bff72007-12-12 05:47:28 +00001506 return InvalidOperands(loc, lex, rex);
Chris Lattner4b009652007-07-25 00:24:17 +00001507}
1508
1509inline QualType Sema::CheckAdditionOperands( // C99 6.5.6
Steve Naroff8f708362007-08-24 19:07:16 +00001510 Expr *&lex, Expr *&rex, SourceLocation loc, bool isCompAssign)
Chris Lattner4b009652007-07-25 00:24:17 +00001511{
1512 if (lex->getType()->isVectorType() || rex->getType()->isVectorType())
1513 return CheckVectorOperands(loc, lex, rex);
1514
Steve Naroff8f708362007-08-24 19:07:16 +00001515 QualType compType = UsualArithmeticConversions(lex, rex, isCompAssign);
Eli Friedmand9b1fec2008-05-18 18:08:51 +00001516
Chris Lattner4b009652007-07-25 00:24:17 +00001517 // handle the common case first (both operands are arithmetic).
1518 if (lex->getType()->isArithmeticType() && rex->getType()->isArithmeticType())
Steve Naroff8f708362007-08-24 19:07:16 +00001519 return compType;
Chris Lattner4b009652007-07-25 00:24:17 +00001520
Eli Friedmand9b1fec2008-05-18 18:08:51 +00001521 // Put any potential pointer into PExp
1522 Expr* PExp = lex, *IExp = rex;
1523 if (IExp->getType()->isPointerType())
1524 std::swap(PExp, IExp);
1525
1526 if (const PointerType* PTy = PExp->getType()->getAsPointerType()) {
1527 if (IExp->getType()->isIntegerType()) {
1528 // Check for arithmetic on pointers to incomplete types
1529 if (!PTy->getPointeeType()->isObjectType()) {
1530 if (PTy->getPointeeType()->isVoidType()) {
1531 Diag(loc, diag::ext_gnu_void_ptr,
1532 lex->getSourceRange(), rex->getSourceRange());
1533 } else {
1534 Diag(loc, diag::err_typecheck_arithmetic_incomplete_type,
1535 lex->getType().getAsString(), lex->getSourceRange());
1536 return QualType();
1537 }
1538 }
1539 return PExp->getType();
1540 }
1541 }
1542
Chris Lattner2c8bff72007-12-12 05:47:28 +00001543 return InvalidOperands(loc, lex, rex);
Chris Lattner4b009652007-07-25 00:24:17 +00001544}
1545
Chris Lattnerfe1f4032008-04-07 05:30:13 +00001546// C99 6.5.6
1547QualType Sema::CheckSubtractionOperands(Expr *&lex, Expr *&rex,
1548 SourceLocation loc, bool isCompAssign) {
Chris Lattner4b009652007-07-25 00:24:17 +00001549 if (lex->getType()->isVectorType() || rex->getType()->isVectorType())
1550 return CheckVectorOperands(loc, lex, rex);
1551
Steve Naroff8f708362007-08-24 19:07:16 +00001552 QualType compType = UsualArithmeticConversions(lex, rex, isCompAssign);
Chris Lattner4b009652007-07-25 00:24:17 +00001553
Chris Lattnerf6da2912007-12-09 21:53:25 +00001554 // Enforce type constraints: C99 6.5.6p3.
1555
1556 // Handle the common case first (both operands are arithmetic).
Chris Lattner4b009652007-07-25 00:24:17 +00001557 if (lex->getType()->isArithmeticType() && rex->getType()->isArithmeticType())
Steve Naroff8f708362007-08-24 19:07:16 +00001558 return compType;
Chris Lattnerf6da2912007-12-09 21:53:25 +00001559
1560 // Either ptr - int or ptr - ptr.
1561 if (const PointerType *LHSPTy = lex->getType()->getAsPointerType()) {
Steve Naroff577f9722008-01-29 18:58:14 +00001562 QualType lpointee = LHSPTy->getPointeeType();
Eli Friedman50727042008-02-08 01:19:44 +00001563
Chris Lattnerf6da2912007-12-09 21:53:25 +00001564 // The LHS must be an object type, not incomplete, function, etc.
Steve Naroff577f9722008-01-29 18:58:14 +00001565 if (!lpointee->isObjectType()) {
Chris Lattnerf6da2912007-12-09 21:53:25 +00001566 // Handle the GNU void* extension.
Steve Naroff577f9722008-01-29 18:58:14 +00001567 if (lpointee->isVoidType()) {
Chris Lattnerf6da2912007-12-09 21:53:25 +00001568 Diag(loc, diag::ext_gnu_void_ptr,
1569 lex->getSourceRange(), rex->getSourceRange());
1570 } else {
1571 Diag(loc, diag::err_typecheck_sub_ptr_object,
1572 lex->getType().getAsString(), lex->getSourceRange());
1573 return QualType();
1574 }
1575 }
1576
1577 // The result type of a pointer-int computation is the pointer type.
1578 if (rex->getType()->isIntegerType())
1579 return lex->getType();
Chris Lattner4b009652007-07-25 00:24:17 +00001580
Chris Lattnerf6da2912007-12-09 21:53:25 +00001581 // Handle pointer-pointer subtractions.
1582 if (const PointerType *RHSPTy = rex->getType()->getAsPointerType()) {
Eli Friedman50727042008-02-08 01:19:44 +00001583 QualType rpointee = RHSPTy->getPointeeType();
1584
Chris Lattnerf6da2912007-12-09 21:53:25 +00001585 // RHS must be an object type, unless void (GNU).
Steve Naroff577f9722008-01-29 18:58:14 +00001586 if (!rpointee->isObjectType()) {
Chris Lattnerf6da2912007-12-09 21:53:25 +00001587 // Handle the GNU void* extension.
Steve Naroff577f9722008-01-29 18:58:14 +00001588 if (rpointee->isVoidType()) {
1589 if (!lpointee->isVoidType())
Chris Lattnerf6da2912007-12-09 21:53:25 +00001590 Diag(loc, diag::ext_gnu_void_ptr,
1591 lex->getSourceRange(), rex->getSourceRange());
1592 } else {
1593 Diag(loc, diag::err_typecheck_sub_ptr_object,
1594 rex->getType().getAsString(), rex->getSourceRange());
1595 return QualType();
1596 }
1597 }
1598
1599 // Pointee types must be compatible.
Steve Naroff577f9722008-01-29 18:58:14 +00001600 if (!Context.typesAreCompatible(lpointee.getUnqualifiedType(),
1601 rpointee.getUnqualifiedType())) {
Chris Lattnerf6da2912007-12-09 21:53:25 +00001602 Diag(loc, diag::err_typecheck_sub_ptr_compatible,
1603 lex->getType().getAsString(), rex->getType().getAsString(),
1604 lex->getSourceRange(), rex->getSourceRange());
1605 return QualType();
1606 }
1607
1608 return Context.getPointerDiffType();
1609 }
1610 }
1611
Chris Lattner2c8bff72007-12-12 05:47:28 +00001612 return InvalidOperands(loc, lex, rex);
Chris Lattner4b009652007-07-25 00:24:17 +00001613}
1614
Chris Lattnerfe1f4032008-04-07 05:30:13 +00001615// C99 6.5.7
1616QualType Sema::CheckShiftOperands(Expr *&lex, Expr *&rex, SourceLocation loc,
1617 bool isCompAssign) {
Chris Lattner2c8bff72007-12-12 05:47:28 +00001618 // C99 6.5.7p2: Each of the operands shall have integer type.
1619 if (!lex->getType()->isIntegerType() || !rex->getType()->isIntegerType())
1620 return InvalidOperands(loc, lex, rex);
Chris Lattner4b009652007-07-25 00:24:17 +00001621
Chris Lattner2c8bff72007-12-12 05:47:28 +00001622 // Shifts don't perform usual arithmetic conversions, they just do integer
1623 // promotions on each operand. C99 6.5.7p3
Chris Lattnerbb19bc42007-12-13 07:28:16 +00001624 if (!isCompAssign)
1625 UsualUnaryConversions(lex);
Chris Lattner2c8bff72007-12-12 05:47:28 +00001626 UsualUnaryConversions(rex);
1627
1628 // "The type of the result is that of the promoted left operand."
1629 return lex->getType();
Chris Lattner4b009652007-07-25 00:24:17 +00001630}
1631
Chris Lattnerfe1f4032008-04-07 05:30:13 +00001632// C99 6.5.8
1633QualType Sema::CheckCompareOperands(Expr *&lex, Expr *&rex, SourceLocation loc,
1634 bool isRelational) {
Chris Lattner254f3bc2007-08-26 01:18:55 +00001635 // C99 6.5.8p3 / C99 6.5.9p4
Steve Naroffecc4fa12007-08-10 18:26:40 +00001636 if (lex->getType()->isArithmeticType() && rex->getType()->isArithmeticType())
1637 UsualArithmeticConversions(lex, rex);
1638 else {
1639 UsualUnaryConversions(lex);
1640 UsualUnaryConversions(rex);
1641 }
Chris Lattner4b009652007-07-25 00:24:17 +00001642 QualType lType = lex->getType();
1643 QualType rType = rex->getType();
1644
Ted Kremenek486509e2007-10-29 17:13:39 +00001645 // For non-floating point types, check for self-comparisons of the form
1646 // x == x, x != x, x < x, etc. These always evaluate to a constant, and
1647 // often indicate logic errors in the program.
Ted Kremenekcf8b77d2007-10-29 16:58:49 +00001648 if (!lType->isFloatingType()) {
Ted Kremenek87e30c52008-01-17 16:57:34 +00001649 if (DeclRefExpr* DRL = dyn_cast<DeclRefExpr>(lex->IgnoreParens()))
1650 if (DeclRefExpr* DRR = dyn_cast<DeclRefExpr>(rex->IgnoreParens()))
Ted Kremenekcf8b77d2007-10-29 16:58:49 +00001651 if (DRL->getDecl() == DRR->getDecl())
1652 Diag(loc, diag::warn_selfcomparison);
1653 }
1654
Chris Lattner254f3bc2007-08-26 01:18:55 +00001655 if (isRelational) {
1656 if (lType->isRealType() && rType->isRealType())
1657 return Context.IntTy;
1658 } else {
Ted Kremenek486509e2007-10-29 17:13:39 +00001659 // Check for comparisons of floating point operands using != and ==.
Ted Kremenek486509e2007-10-29 17:13:39 +00001660 if (lType->isFloatingType()) {
1661 assert (rType->isFloatingType());
Ted Kremenek30c66752007-11-25 00:58:00 +00001662 CheckFloatComparison(loc,lex,rex);
Ted Kremenek75439142007-10-29 16:40:01 +00001663 }
1664
Chris Lattner254f3bc2007-08-26 01:18:55 +00001665 if (lType->isArithmeticType() && rType->isArithmeticType())
1666 return Context.IntTy;
1667 }
Chris Lattner4b009652007-07-25 00:24:17 +00001668
Chris Lattner22be8422007-08-26 01:10:14 +00001669 bool LHSIsNull = lex->isNullPointerConstant(Context);
1670 bool RHSIsNull = rex->isNullPointerConstant(Context);
1671
Chris Lattner254f3bc2007-08-26 01:18:55 +00001672 // All of the following pointer related warnings are GCC extensions, except
1673 // when handling null pointer constants. One day, we can consider making them
1674 // errors (when -pedantic-errors is enabled).
Steve Naroffc33c0602007-08-27 04:08:11 +00001675 if (lType->isPointerType() && rType->isPointerType()) { // C99 6.5.8p2
Chris Lattner56a5cd62008-04-03 05:07:25 +00001676 QualType LCanPointeeTy =
1677 lType->getAsPointerType()->getPointeeType().getCanonicalType();
1678 QualType RCanPointeeTy =
1679 rType->getAsPointerType()->getPointeeType().getCanonicalType();
Eli Friedman50727042008-02-08 01:19:44 +00001680
Steve Naroff3b435622007-11-13 14:57:38 +00001681 if (!LHSIsNull && !RHSIsNull && // C99 6.5.9p2
Chris Lattner56a5cd62008-04-03 05:07:25 +00001682 !LCanPointeeTy->isVoidType() && !RCanPointeeTy->isVoidType() &&
1683 !Context.typesAreCompatible(LCanPointeeTy.getUnqualifiedType(),
1684 RCanPointeeTy.getUnqualifiedType())) {
Steve Naroff4462cb02007-08-16 21:48:38 +00001685 Diag(loc, diag::ext_typecheck_comparison_of_distinct_pointers,
1686 lType.getAsString(), rType.getAsString(),
1687 lex->getSourceRange(), rex->getSourceRange());
Chris Lattner4b009652007-07-25 00:24:17 +00001688 }
Chris Lattnere992d6c2008-01-16 19:17:22 +00001689 ImpCastExprToType(rex, lType); // promote the pointer to pointer
Steve Naroff4462cb02007-08-16 21:48:38 +00001690 return Context.IntTy;
1691 }
Steve Naroff936c4362008-06-03 14:04:54 +00001692 if ((lType->isObjCQualifiedIdType() || rType->isObjCQualifiedIdType())) {
1693 if (ObjCQualifiedIdTypesAreCompatible(lType, rType, true)) {
1694 ImpCastExprToType(rex, lType);
1695 return Context.IntTy;
1696 }
Fariborz Jahanian5319d9c2007-12-20 01:06:58 +00001697 }
Steve Naroff936c4362008-06-03 14:04:54 +00001698 if ((lType->isPointerType() || lType->isObjCQualifiedIdType()) &&
1699 rType->isIntegerType()) {
Chris Lattner22be8422007-08-26 01:10:14 +00001700 if (!RHSIsNull)
Steve Naroff4462cb02007-08-16 21:48:38 +00001701 Diag(loc, diag::ext_typecheck_comparison_of_pointer_integer,
1702 lType.getAsString(), rType.getAsString(),
1703 lex->getSourceRange(), rex->getSourceRange());
Chris Lattnere992d6c2008-01-16 19:17:22 +00001704 ImpCastExprToType(rex, lType); // promote the integer to pointer
Steve Naroff4462cb02007-08-16 21:48:38 +00001705 return Context.IntTy;
1706 }
Steve Naroff936c4362008-06-03 14:04:54 +00001707 if (lType->isIntegerType() &&
1708 (rType->isPointerType() || rType->isObjCQualifiedIdType())) {
Chris Lattner22be8422007-08-26 01:10:14 +00001709 if (!LHSIsNull)
Steve Naroff4462cb02007-08-16 21:48:38 +00001710 Diag(loc, diag::ext_typecheck_comparison_of_pointer_integer,
1711 lType.getAsString(), rType.getAsString(),
1712 lex->getSourceRange(), rex->getSourceRange());
Chris Lattnere992d6c2008-01-16 19:17:22 +00001713 ImpCastExprToType(lex, rType); // promote the integer to pointer
Steve Naroff4462cb02007-08-16 21:48:38 +00001714 return Context.IntTy;
Chris Lattner4b009652007-07-25 00:24:17 +00001715 }
Chris Lattner2c8bff72007-12-12 05:47:28 +00001716 return InvalidOperands(loc, lex, rex);
Chris Lattner4b009652007-07-25 00:24:17 +00001717}
1718
Chris Lattner4b009652007-07-25 00:24:17 +00001719inline QualType Sema::CheckBitwiseOperands(
Steve Naroff8f708362007-08-24 19:07:16 +00001720 Expr *&lex, Expr *&rex, SourceLocation loc, bool isCompAssign)
Chris Lattner4b009652007-07-25 00:24:17 +00001721{
1722 if (lex->getType()->isVectorType() || rex->getType()->isVectorType())
1723 return CheckVectorOperands(loc, lex, rex);
1724
Steve Naroff8f708362007-08-24 19:07:16 +00001725 QualType compType = UsualArithmeticConversions(lex, rex, isCompAssign);
Chris Lattner4b009652007-07-25 00:24:17 +00001726
1727 if (lex->getType()->isIntegerType() && rex->getType()->isIntegerType())
Steve Naroff8f708362007-08-24 19:07:16 +00001728 return compType;
Chris Lattner2c8bff72007-12-12 05:47:28 +00001729 return InvalidOperands(loc, lex, rex);
Chris Lattner4b009652007-07-25 00:24:17 +00001730}
1731
1732inline QualType Sema::CheckLogicalOperands( // C99 6.5.[13,14]
1733 Expr *&lex, Expr *&rex, SourceLocation loc)
1734{
1735 UsualUnaryConversions(lex);
1736 UsualUnaryConversions(rex);
1737
Eli Friedmanbea3f842008-05-13 20:16:47 +00001738 if (lex->getType()->isScalarType() && rex->getType()->isScalarType())
Chris Lattner4b009652007-07-25 00:24:17 +00001739 return Context.IntTy;
Chris Lattner2c8bff72007-12-12 05:47:28 +00001740 return InvalidOperands(loc, lex, rex);
Chris Lattner4b009652007-07-25 00:24:17 +00001741}
1742
1743inline QualType Sema::CheckAssignmentOperands( // C99 6.5.16.1
Steve Naroff0f32f432007-08-24 22:33:52 +00001744 Expr *lex, Expr *&rex, SourceLocation loc, QualType compoundType)
Chris Lattner4b009652007-07-25 00:24:17 +00001745{
1746 QualType lhsType = lex->getType();
1747 QualType rhsType = compoundType.isNull() ? rex->getType() : compoundType;
Chris Lattner4b009652007-07-25 00:24:17 +00001748 Expr::isModifiableLvalueResult mlval = lex->isModifiableLvalue();
1749
1750 switch (mlval) { // C99 6.5.16p2
Chris Lattner005ed752008-01-04 18:04:52 +00001751 case Expr::MLV_Valid:
1752 break;
1753 case Expr::MLV_ConstQualified:
1754 Diag(loc, diag::err_typecheck_assign_const, lex->getSourceRange());
1755 return QualType();
1756 case Expr::MLV_ArrayType:
1757 Diag(loc, diag::err_typecheck_array_not_modifiable_lvalue,
1758 lhsType.getAsString(), lex->getSourceRange());
1759 return QualType();
1760 case Expr::MLV_NotObjectType:
1761 Diag(loc, diag::err_typecheck_non_object_not_modifiable_lvalue,
1762 lhsType.getAsString(), lex->getSourceRange());
1763 return QualType();
1764 case Expr::MLV_InvalidExpression:
1765 Diag(loc, diag::err_typecheck_expression_not_modifiable_lvalue,
1766 lex->getSourceRange());
1767 return QualType();
1768 case Expr::MLV_IncompleteType:
1769 case Expr::MLV_IncompleteVoidType:
1770 Diag(loc, diag::err_typecheck_incomplete_type_not_modifiable_lvalue,
1771 lhsType.getAsString(), lex->getSourceRange());
1772 return QualType();
1773 case Expr::MLV_DuplicateVectorComponents:
1774 Diag(loc, diag::err_typecheck_duplicate_vector_components_not_mlvalue,
1775 lex->getSourceRange());
1776 return QualType();
Chris Lattner4b009652007-07-25 00:24:17 +00001777 }
Steve Naroff7cbb1462007-07-31 12:34:36 +00001778
Chris Lattner005ed752008-01-04 18:04:52 +00001779 AssignConvertType ConvTy;
1780 if (compoundType.isNull())
1781 ConvTy = CheckSingleAssignmentConstraints(lhsType, rex);
1782 else
1783 ConvTy = CheckCompoundAssignmentConstraints(lhsType, rhsType);
1784
1785 if (DiagnoseAssignmentResult(ConvTy, loc, lhsType, rhsType,
1786 rex, "assigning"))
1787 return QualType();
1788
Chris Lattner4b009652007-07-25 00:24:17 +00001789 // C99 6.5.16p3: The type of an assignment expression is the type of the
1790 // left operand unless the left operand has qualified type, in which case
1791 // it is the unqualified version of the type of the left operand.
1792 // C99 6.5.16.1p2: In simple assignment, the value of the right operand
1793 // is converted to the type of the assignment expression (above).
Chris Lattner0d9bcea2007-08-30 17:45:32 +00001794 // C++ 5.17p1: the type of the assignment expression is that of its left
1795 // oprdu.
Chris Lattner005ed752008-01-04 18:04:52 +00001796 return lhsType.getUnqualifiedType();
Chris Lattner4b009652007-07-25 00:24:17 +00001797}
1798
1799inline QualType Sema::CheckCommaOperands( // C99 6.5.17
1800 Expr *&lex, Expr *&rex, SourceLocation loc) {
1801 UsualUnaryConversions(rex);
1802 return rex->getType();
1803}
1804
1805/// CheckIncrementDecrementOperand - unlike most "Check" methods, this routine
1806/// doesn't need to call UsualUnaryConversions or UsualArithmeticConversions.
1807QualType Sema::CheckIncrementDecrementOperand(Expr *op, SourceLocation OpLoc) {
1808 QualType resType = op->getType();
1809 assert(!resType.isNull() && "no type for increment/decrement expression");
1810
Steve Naroffd30e1932007-08-24 17:20:07 +00001811 // C99 6.5.2.4p1: We allow complex as a GCC extension.
Steve Naroffce827582007-11-11 14:15:57 +00001812 if (const PointerType *pt = resType->getAsPointerType()) {
Eli Friedmand9b1fec2008-05-18 18:08:51 +00001813 if (pt->getPointeeType()->isVoidType()) {
1814 Diag(OpLoc, diag::ext_gnu_void_ptr, op->getSourceRange());
1815 } else if (!pt->getPointeeType()->isObjectType()) {
1816 // C99 6.5.2.4p2, 6.5.6p2
Chris Lattner4b009652007-07-25 00:24:17 +00001817 Diag(OpLoc, diag::err_typecheck_arithmetic_incomplete_type,
1818 resType.getAsString(), op->getSourceRange());
1819 return QualType();
1820 }
Steve Naroffd30e1932007-08-24 17:20:07 +00001821 } else if (!resType->isRealType()) {
1822 if (resType->isComplexType())
1823 // C99 does not support ++/-- on complex types.
1824 Diag(OpLoc, diag::ext_integer_increment_complex,
1825 resType.getAsString(), op->getSourceRange());
1826 else {
1827 Diag(OpLoc, diag::err_typecheck_illegal_increment_decrement,
1828 resType.getAsString(), op->getSourceRange());
1829 return QualType();
1830 }
Chris Lattner4b009652007-07-25 00:24:17 +00001831 }
Steve Naroff6acc0f42007-08-23 21:37:33 +00001832 // At this point, we know we have a real, complex or pointer type.
1833 // Now make sure the operand is a modifiable lvalue.
Chris Lattner4b009652007-07-25 00:24:17 +00001834 Expr::isModifiableLvalueResult mlval = op->isModifiableLvalue();
1835 if (mlval != Expr::MLV_Valid) {
1836 // FIXME: emit a more precise diagnostic...
1837 Diag(OpLoc, diag::err_typecheck_invalid_lvalue_incr_decr,
1838 op->getSourceRange());
1839 return QualType();
1840 }
1841 return resType;
1842}
1843
Anders Carlsson4b3db2b2008-02-01 07:15:58 +00001844/// getPrimaryDecl - Helper function for CheckAddressOfOperand().
Chris Lattner4b009652007-07-25 00:24:17 +00001845/// This routine allows us to typecheck complex/recursive expressions
1846/// where the declaration is needed for type checking. Here are some
1847/// examples: &s.xx, &s.zz[1].yy, &(1+2), &(XX), &"123"[2].
Chris Lattner48d7f382008-04-02 04:24:33 +00001848static ValueDecl *getPrimaryDecl(Expr *E) {
1849 switch (E->getStmtClass()) {
Chris Lattner4b009652007-07-25 00:24:17 +00001850 case Stmt::DeclRefExprClass:
Chris Lattner48d7f382008-04-02 04:24:33 +00001851 return cast<DeclRefExpr>(E)->getDecl();
Chris Lattner4b009652007-07-25 00:24:17 +00001852 case Stmt::MemberExprClass:
Chris Lattnera3249072007-11-16 17:46:48 +00001853 // Fields cannot be declared with a 'register' storage class.
1854 // &X->f is always ok, even if X is declared register.
Chris Lattner48d7f382008-04-02 04:24:33 +00001855 if (cast<MemberExpr>(E)->isArrow())
Chris Lattnera3249072007-11-16 17:46:48 +00001856 return 0;
Chris Lattner48d7f382008-04-02 04:24:33 +00001857 return getPrimaryDecl(cast<MemberExpr>(E)->getBase());
Anders Carlsson4b3db2b2008-02-01 07:15:58 +00001858 case Stmt::ArraySubscriptExprClass: {
1859 // &X[4] and &4[X] is invalid if X is invalid and X is not a pointer.
1860
Chris Lattner48d7f382008-04-02 04:24:33 +00001861 ValueDecl *VD = getPrimaryDecl(cast<ArraySubscriptExpr>(E)->getBase());
Anders Carlsson655694e2008-02-01 16:01:31 +00001862 if (!VD || VD->getType()->isPointerType())
Anders Carlsson4b3db2b2008-02-01 07:15:58 +00001863 return 0;
1864 else
1865 return VD;
1866 }
Chris Lattner4b009652007-07-25 00:24:17 +00001867 case Stmt::UnaryOperatorClass:
Chris Lattner48d7f382008-04-02 04:24:33 +00001868 return getPrimaryDecl(cast<UnaryOperator>(E)->getSubExpr());
Chris Lattner4b009652007-07-25 00:24:17 +00001869 case Stmt::ParenExprClass:
Chris Lattner48d7f382008-04-02 04:24:33 +00001870 return getPrimaryDecl(cast<ParenExpr>(E)->getSubExpr());
Chris Lattnera3249072007-11-16 17:46:48 +00001871 case Stmt::ImplicitCastExprClass:
1872 // &X[4] when X is an array, has an implicit cast from array to pointer.
Chris Lattner48d7f382008-04-02 04:24:33 +00001873 return getPrimaryDecl(cast<ImplicitCastExpr>(E)->getSubExpr());
Chris Lattner4b009652007-07-25 00:24:17 +00001874 default:
1875 return 0;
1876 }
1877}
1878
1879/// CheckAddressOfOperand - The operand of & must be either a function
1880/// designator or an lvalue designating an object. If it is an lvalue, the
1881/// object cannot be declared with storage class register or be a bit field.
1882/// Note: The usual conversions are *not* applied to the operand of the &
1883/// operator (C99 6.3.2.1p[2-4]), and its result is never an lvalue.
1884QualType Sema::CheckAddressOfOperand(Expr *op, SourceLocation OpLoc) {
Steve Naroff9c6c3592008-01-13 17:10:08 +00001885 if (getLangOptions().C99) {
1886 // Implement C99-only parts of addressof rules.
1887 if (UnaryOperator* uOp = dyn_cast<UnaryOperator>(op)) {
1888 if (uOp->getOpcode() == UnaryOperator::Deref)
1889 // Per C99 6.5.3.2, the address of a deref always returns a valid result
1890 // (assuming the deref expression is valid).
1891 return uOp->getSubExpr()->getType();
1892 }
1893 // Technically, there should be a check for array subscript
1894 // expressions here, but the result of one is always an lvalue anyway.
1895 }
Anders Carlsson4b3db2b2008-02-01 07:15:58 +00001896 ValueDecl *dcl = getPrimaryDecl(op);
Chris Lattner4b009652007-07-25 00:24:17 +00001897 Expr::isLvalueResult lval = op->isLvalue();
1898
1899 if (lval != Expr::LV_Valid) { // C99 6.5.3.2p1
Chris Lattnera3249072007-11-16 17:46:48 +00001900 if (!dcl || !isa<FunctionDecl>(dcl)) {// allow function designators
1901 // FIXME: emit more specific diag...
Chris Lattner4b009652007-07-25 00:24:17 +00001902 Diag(OpLoc, diag::err_typecheck_invalid_lvalue_addrof,
1903 op->getSourceRange());
1904 return QualType();
1905 }
Steve Naroff73cf87e2008-02-29 23:30:25 +00001906 } else if (MemberExpr *MemExpr = dyn_cast<MemberExpr>(op)) { // C99 6.5.3.2p1
1907 if (MemExpr->getMemberDecl()->isBitField()) {
1908 Diag(OpLoc, diag::err_typecheck_address_of,
1909 std::string("bit-field"), op->getSourceRange());
1910 return QualType();
1911 }
1912 // Check for Apple extension for accessing vector components.
1913 } else if (isa<ArraySubscriptExpr>(op) &&
1914 cast<ArraySubscriptExpr>(op)->getBase()->getType()->isVectorType()) {
1915 Diag(OpLoc, diag::err_typecheck_address_of,
1916 std::string("vector"), op->getSourceRange());
1917 return QualType();
1918 } else if (dcl) { // C99 6.5.3.2p1
Chris Lattner4b009652007-07-25 00:24:17 +00001919 // We have an lvalue with a decl. Make sure the decl is not declared
1920 // with the register storage-class specifier.
1921 if (const VarDecl *vd = dyn_cast<VarDecl>(dcl)) {
1922 if (vd->getStorageClass() == VarDecl::Register) {
Steve Naroff73cf87e2008-02-29 23:30:25 +00001923 Diag(OpLoc, diag::err_typecheck_address_of,
1924 std::string("register variable"), op->getSourceRange());
Chris Lattner4b009652007-07-25 00:24:17 +00001925 return QualType();
1926 }
1927 } else
1928 assert(0 && "Unknown/unexpected decl type");
Chris Lattner4b009652007-07-25 00:24:17 +00001929 }
1930 // If the operand has type "type", the result has type "pointer to type".
1931 return Context.getPointerType(op->getType());
1932}
1933
1934QualType Sema::CheckIndirectionOperand(Expr *op, SourceLocation OpLoc) {
1935 UsualUnaryConversions(op);
1936 QualType qType = op->getType();
1937
Chris Lattner7931f4a2007-07-31 16:53:04 +00001938 if (const PointerType *PT = qType->getAsPointerType()) {
Steve Naroff9c6c3592008-01-13 17:10:08 +00001939 // Note that per both C89 and C99, this is always legal, even
1940 // if ptype is an incomplete type or void.
1941 // It would be possible to warn about dereferencing a
1942 // void pointer, but it's completely well-defined,
1943 // and such a warning is unlikely to catch any mistakes.
1944 return PT->getPointeeType();
Chris Lattner4b009652007-07-25 00:24:17 +00001945 }
1946 Diag(OpLoc, diag::err_typecheck_indirection_requires_pointer,
1947 qType.getAsString(), op->getSourceRange());
1948 return QualType();
1949}
1950
1951static inline BinaryOperator::Opcode ConvertTokenKindToBinaryOpcode(
1952 tok::TokenKind Kind) {
1953 BinaryOperator::Opcode Opc;
1954 switch (Kind) {
1955 default: assert(0 && "Unknown binop!");
1956 case tok::star: Opc = BinaryOperator::Mul; break;
1957 case tok::slash: Opc = BinaryOperator::Div; break;
1958 case tok::percent: Opc = BinaryOperator::Rem; break;
1959 case tok::plus: Opc = BinaryOperator::Add; break;
1960 case tok::minus: Opc = BinaryOperator::Sub; break;
1961 case tok::lessless: Opc = BinaryOperator::Shl; break;
1962 case tok::greatergreater: Opc = BinaryOperator::Shr; break;
1963 case tok::lessequal: Opc = BinaryOperator::LE; break;
1964 case tok::less: Opc = BinaryOperator::LT; break;
1965 case tok::greaterequal: Opc = BinaryOperator::GE; break;
1966 case tok::greater: Opc = BinaryOperator::GT; break;
1967 case tok::exclaimequal: Opc = BinaryOperator::NE; break;
1968 case tok::equalequal: Opc = BinaryOperator::EQ; break;
1969 case tok::amp: Opc = BinaryOperator::And; break;
1970 case tok::caret: Opc = BinaryOperator::Xor; break;
1971 case tok::pipe: Opc = BinaryOperator::Or; break;
1972 case tok::ampamp: Opc = BinaryOperator::LAnd; break;
1973 case tok::pipepipe: Opc = BinaryOperator::LOr; break;
1974 case tok::equal: Opc = BinaryOperator::Assign; break;
1975 case tok::starequal: Opc = BinaryOperator::MulAssign; break;
1976 case tok::slashequal: Opc = BinaryOperator::DivAssign; break;
1977 case tok::percentequal: Opc = BinaryOperator::RemAssign; break;
1978 case tok::plusequal: Opc = BinaryOperator::AddAssign; break;
1979 case tok::minusequal: Opc = BinaryOperator::SubAssign; break;
1980 case tok::lesslessequal: Opc = BinaryOperator::ShlAssign; break;
1981 case tok::greatergreaterequal: Opc = BinaryOperator::ShrAssign; break;
1982 case tok::ampequal: Opc = BinaryOperator::AndAssign; break;
1983 case tok::caretequal: Opc = BinaryOperator::XorAssign; break;
1984 case tok::pipeequal: Opc = BinaryOperator::OrAssign; break;
1985 case tok::comma: Opc = BinaryOperator::Comma; break;
1986 }
1987 return Opc;
1988}
1989
1990static inline UnaryOperator::Opcode ConvertTokenKindToUnaryOpcode(
1991 tok::TokenKind Kind) {
1992 UnaryOperator::Opcode Opc;
1993 switch (Kind) {
1994 default: assert(0 && "Unknown unary op!");
1995 case tok::plusplus: Opc = UnaryOperator::PreInc; break;
1996 case tok::minusminus: Opc = UnaryOperator::PreDec; break;
1997 case tok::amp: Opc = UnaryOperator::AddrOf; break;
1998 case tok::star: Opc = UnaryOperator::Deref; break;
1999 case tok::plus: Opc = UnaryOperator::Plus; break;
2000 case tok::minus: Opc = UnaryOperator::Minus; break;
2001 case tok::tilde: Opc = UnaryOperator::Not; break;
2002 case tok::exclaim: Opc = UnaryOperator::LNot; break;
2003 case tok::kw_sizeof: Opc = UnaryOperator::SizeOf; break;
2004 case tok::kw___alignof: Opc = UnaryOperator::AlignOf; break;
2005 case tok::kw___real: Opc = UnaryOperator::Real; break;
2006 case tok::kw___imag: Opc = UnaryOperator::Imag; break;
2007 case tok::kw___extension__: Opc = UnaryOperator::Extension; break;
2008 }
2009 return Opc;
2010}
2011
2012// Binary Operators. 'Tok' is the token for the operator.
Steve Naroff87d58b42007-09-16 03:34:24 +00002013Action::ExprResult Sema::ActOnBinOp(SourceLocation TokLoc, tok::TokenKind Kind,
Chris Lattner4b009652007-07-25 00:24:17 +00002014 ExprTy *LHS, ExprTy *RHS) {
2015 BinaryOperator::Opcode Opc = ConvertTokenKindToBinaryOpcode(Kind);
2016 Expr *lhs = (Expr *)LHS, *rhs = (Expr*)RHS;
2017
Steve Naroff87d58b42007-09-16 03:34:24 +00002018 assert((lhs != 0) && "ActOnBinOp(): missing left expression");
2019 assert((rhs != 0) && "ActOnBinOp(): missing right expression");
Chris Lattner4b009652007-07-25 00:24:17 +00002020
2021 QualType ResultTy; // Result type of the binary operator.
2022 QualType CompTy; // Computation type for compound assignments (e.g. '+=')
2023
2024 switch (Opc) {
2025 default:
2026 assert(0 && "Unknown binary expr!");
2027 case BinaryOperator::Assign:
2028 ResultTy = CheckAssignmentOperands(lhs, rhs, TokLoc, QualType());
2029 break;
2030 case BinaryOperator::Mul:
2031 case BinaryOperator::Div:
2032 ResultTy = CheckMultiplyDivideOperands(lhs, rhs, TokLoc);
2033 break;
2034 case BinaryOperator::Rem:
2035 ResultTy = CheckRemainderOperands(lhs, rhs, TokLoc);
2036 break;
2037 case BinaryOperator::Add:
2038 ResultTy = CheckAdditionOperands(lhs, rhs, TokLoc);
2039 break;
2040 case BinaryOperator::Sub:
2041 ResultTy = CheckSubtractionOperands(lhs, rhs, TokLoc);
2042 break;
2043 case BinaryOperator::Shl:
2044 case BinaryOperator::Shr:
2045 ResultTy = CheckShiftOperands(lhs, rhs, TokLoc);
2046 break;
2047 case BinaryOperator::LE:
2048 case BinaryOperator::LT:
2049 case BinaryOperator::GE:
2050 case BinaryOperator::GT:
Chris Lattner254f3bc2007-08-26 01:18:55 +00002051 ResultTy = CheckCompareOperands(lhs, rhs, TokLoc, true);
Chris Lattner4b009652007-07-25 00:24:17 +00002052 break;
2053 case BinaryOperator::EQ:
2054 case BinaryOperator::NE:
Chris Lattner254f3bc2007-08-26 01:18:55 +00002055 ResultTy = CheckCompareOperands(lhs, rhs, TokLoc, false);
Chris Lattner4b009652007-07-25 00:24:17 +00002056 break;
2057 case BinaryOperator::And:
2058 case BinaryOperator::Xor:
2059 case BinaryOperator::Or:
2060 ResultTy = CheckBitwiseOperands(lhs, rhs, TokLoc);
2061 break;
2062 case BinaryOperator::LAnd:
2063 case BinaryOperator::LOr:
2064 ResultTy = CheckLogicalOperands(lhs, rhs, TokLoc);
2065 break;
2066 case BinaryOperator::MulAssign:
2067 case BinaryOperator::DivAssign:
Steve Naroff8f708362007-08-24 19:07:16 +00002068 CompTy = CheckMultiplyDivideOperands(lhs, rhs, TokLoc, true);
Chris Lattner4b009652007-07-25 00:24:17 +00002069 if (!CompTy.isNull())
2070 ResultTy = CheckAssignmentOperands(lhs, rhs, TokLoc, CompTy);
2071 break;
2072 case BinaryOperator::RemAssign:
Steve Naroff8f708362007-08-24 19:07:16 +00002073 CompTy = CheckRemainderOperands(lhs, rhs, TokLoc, true);
Chris Lattner4b009652007-07-25 00:24:17 +00002074 if (!CompTy.isNull())
2075 ResultTy = CheckAssignmentOperands(lhs, rhs, TokLoc, CompTy);
2076 break;
2077 case BinaryOperator::AddAssign:
Steve Naroff8f708362007-08-24 19:07:16 +00002078 CompTy = CheckAdditionOperands(lhs, rhs, TokLoc, true);
Chris Lattner4b009652007-07-25 00:24:17 +00002079 if (!CompTy.isNull())
2080 ResultTy = CheckAssignmentOperands(lhs, rhs, TokLoc, CompTy);
2081 break;
2082 case BinaryOperator::SubAssign:
Steve Naroff8f708362007-08-24 19:07:16 +00002083 CompTy = CheckSubtractionOperands(lhs, rhs, TokLoc, true);
Chris Lattner4b009652007-07-25 00:24:17 +00002084 if (!CompTy.isNull())
2085 ResultTy = CheckAssignmentOperands(lhs, rhs, TokLoc, CompTy);
2086 break;
2087 case BinaryOperator::ShlAssign:
2088 case BinaryOperator::ShrAssign:
Steve Naroff8f708362007-08-24 19:07:16 +00002089 CompTy = CheckShiftOperands(lhs, rhs, TokLoc, true);
Chris Lattner4b009652007-07-25 00:24:17 +00002090 if (!CompTy.isNull())
2091 ResultTy = CheckAssignmentOperands(lhs, rhs, TokLoc, CompTy);
2092 break;
2093 case BinaryOperator::AndAssign:
2094 case BinaryOperator::XorAssign:
2095 case BinaryOperator::OrAssign:
Steve Naroff8f708362007-08-24 19:07:16 +00002096 CompTy = CheckBitwiseOperands(lhs, rhs, TokLoc, true);
Chris Lattner4b009652007-07-25 00:24:17 +00002097 if (!CompTy.isNull())
2098 ResultTy = CheckAssignmentOperands(lhs, rhs, TokLoc, CompTy);
2099 break;
2100 case BinaryOperator::Comma:
2101 ResultTy = CheckCommaOperands(lhs, rhs, TokLoc);
2102 break;
2103 }
2104 if (ResultTy.isNull())
2105 return true;
2106 if (CompTy.isNull())
Chris Lattnerf420df12007-08-28 18:36:55 +00002107 return new BinaryOperator(lhs, rhs, Opc, ResultTy, TokLoc);
Chris Lattner4b009652007-07-25 00:24:17 +00002108 else
Chris Lattnerf420df12007-08-28 18:36:55 +00002109 return new CompoundAssignOperator(lhs, rhs, Opc, ResultTy, CompTy, TokLoc);
Chris Lattner4b009652007-07-25 00:24:17 +00002110}
2111
2112// Unary Operators. 'Tok' is the token for the operator.
Steve Naroff87d58b42007-09-16 03:34:24 +00002113Action::ExprResult Sema::ActOnUnaryOp(SourceLocation OpLoc, tok::TokenKind Op,
Chris Lattner4b009652007-07-25 00:24:17 +00002114 ExprTy *input) {
2115 Expr *Input = (Expr*)input;
2116 UnaryOperator::Opcode Opc = ConvertTokenKindToUnaryOpcode(Op);
2117 QualType resultType;
2118 switch (Opc) {
2119 default:
2120 assert(0 && "Unimplemented unary expr!");
2121 case UnaryOperator::PreInc:
2122 case UnaryOperator::PreDec:
2123 resultType = CheckIncrementDecrementOperand(Input, OpLoc);
2124 break;
2125 case UnaryOperator::AddrOf:
2126 resultType = CheckAddressOfOperand(Input, OpLoc);
2127 break;
2128 case UnaryOperator::Deref:
Steve Naroffccc26a72007-12-18 04:06:57 +00002129 DefaultFunctionArrayConversion(Input);
Chris Lattner4b009652007-07-25 00:24:17 +00002130 resultType = CheckIndirectionOperand(Input, OpLoc);
2131 break;
2132 case UnaryOperator::Plus:
2133 case UnaryOperator::Minus:
2134 UsualUnaryConversions(Input);
2135 resultType = Input->getType();
2136 if (!resultType->isArithmeticType()) // C99 6.5.3.3p1
2137 return Diag(OpLoc, diag::err_typecheck_unary_expr,
2138 resultType.getAsString());
2139 break;
2140 case UnaryOperator::Not: // bitwise complement
2141 UsualUnaryConversions(Input);
2142 resultType = Input->getType();
Steve Naroffd30e1932007-08-24 17:20:07 +00002143 // C99 6.5.3.3p1. We allow complex as a GCC extension.
2144 if (!resultType->isIntegerType()) {
2145 if (resultType->isComplexType())
2146 // C99 does not support '~' for complex conjugation.
2147 Diag(OpLoc, diag::ext_integer_complement_complex,
2148 resultType.getAsString());
2149 else
2150 return Diag(OpLoc, diag::err_typecheck_unary_expr,
2151 resultType.getAsString());
2152 }
Chris Lattner4b009652007-07-25 00:24:17 +00002153 break;
2154 case UnaryOperator::LNot: // logical negation
2155 // Unlike +/-/~, integer promotions aren't done here (C99 6.5.3.3p5).
2156 DefaultFunctionArrayConversion(Input);
2157 resultType = Input->getType();
2158 if (!resultType->isScalarType()) // C99 6.5.3.3p1
2159 return Diag(OpLoc, diag::err_typecheck_unary_expr,
2160 resultType.getAsString());
2161 // LNot always has type int. C99 6.5.3.3p5.
2162 resultType = Context.IntTy;
2163 break;
2164 case UnaryOperator::SizeOf:
2165 resultType = CheckSizeOfAlignOfOperand(Input->getType(), OpLoc, true);
2166 break;
2167 case UnaryOperator::AlignOf:
2168 resultType = CheckSizeOfAlignOfOperand(Input->getType(), OpLoc, false);
2169 break;
Chris Lattner03931a72007-08-24 21:16:53 +00002170 case UnaryOperator::Real:
Chris Lattner03931a72007-08-24 21:16:53 +00002171 case UnaryOperator::Imag:
Chris Lattner5110ad52007-08-24 21:41:10 +00002172 resultType = CheckRealImagOperand(Input, OpLoc);
Chris Lattner03931a72007-08-24 21:16:53 +00002173 break;
Chris Lattner4b009652007-07-25 00:24:17 +00002174 case UnaryOperator::Extension:
Chris Lattner4b009652007-07-25 00:24:17 +00002175 resultType = Input->getType();
2176 break;
2177 }
2178 if (resultType.isNull())
2179 return true;
2180 return new UnaryOperator(Input, Opc, resultType, OpLoc);
2181}
2182
Steve Naroff5cbb02f2007-09-16 14:56:35 +00002183/// ActOnAddrLabel - Parse the GNU address of label extension: "&&foo".
2184Sema::ExprResult Sema::ActOnAddrLabel(SourceLocation OpLoc,
Chris Lattner4b009652007-07-25 00:24:17 +00002185 SourceLocation LabLoc,
2186 IdentifierInfo *LabelII) {
2187 // Look up the record for this label identifier.
2188 LabelStmt *&LabelDecl = LabelMap[LabelII];
2189
2190 // If we haven't seen this label yet, create a forward reference.
2191 if (LabelDecl == 0)
2192 LabelDecl = new LabelStmt(LabLoc, LabelII, 0);
2193
2194 // Create the AST node. The address of a label always has type 'void*'.
Chris Lattnera0d03a72007-08-03 17:31:20 +00002195 return new AddrLabelExpr(OpLoc, LabLoc, LabelDecl,
2196 Context.getPointerType(Context.VoidTy));
Chris Lattner4b009652007-07-25 00:24:17 +00002197}
2198
Steve Naroff5cbb02f2007-09-16 14:56:35 +00002199Sema::ExprResult Sema::ActOnStmtExpr(SourceLocation LPLoc, StmtTy *substmt,
Chris Lattner4b009652007-07-25 00:24:17 +00002200 SourceLocation RPLoc) { // "({..})"
2201 Stmt *SubStmt = static_cast<Stmt*>(substmt);
2202 assert(SubStmt && isa<CompoundStmt>(SubStmt) && "Invalid action invocation!");
2203 CompoundStmt *Compound = cast<CompoundStmt>(SubStmt);
2204
2205 // FIXME: there are a variety of strange constraints to enforce here, for
2206 // example, it is not possible to goto into a stmt expression apparently.
2207 // More semantic analysis is needed.
2208
2209 // FIXME: the last statement in the compount stmt has its value used. We
2210 // should not warn about it being unused.
2211
2212 // If there are sub stmts in the compound stmt, take the type of the last one
2213 // as the type of the stmtexpr.
2214 QualType Ty = Context.VoidTy;
2215
2216 if (!Compound->body_empty())
2217 if (Expr *LastExpr = dyn_cast<Expr>(Compound->body_back()))
2218 Ty = LastExpr->getType();
2219
2220 return new StmtExpr(Compound, Ty, LPLoc, RPLoc);
2221}
Steve Naroff63bad2d2007-08-01 22:05:33 +00002222
Steve Naroff5cbb02f2007-09-16 14:56:35 +00002223Sema::ExprResult Sema::ActOnBuiltinOffsetOf(SourceLocation BuiltinLoc,
Chris Lattner0d9bcea2007-08-30 17:45:32 +00002224 SourceLocation TypeLoc,
2225 TypeTy *argty,
2226 OffsetOfComponent *CompPtr,
2227 unsigned NumComponents,
2228 SourceLocation RPLoc) {
2229 QualType ArgTy = QualType::getFromOpaquePtr(argty);
2230 assert(!ArgTy.isNull() && "Missing type argument!");
2231
2232 // We must have at least one component that refers to the type, and the first
2233 // one is known to be a field designator. Verify that the ArgTy represents
2234 // a struct/union/class.
2235 if (!ArgTy->isRecordType())
2236 return Diag(TypeLoc, diag::err_offsetof_record_type,ArgTy.getAsString());
2237
2238 // Otherwise, create a compound literal expression as the base, and
2239 // iteratively process the offsetof designators.
Steve Naroffbe37fc02008-01-14 18:19:28 +00002240 Expr *Res = new CompoundLiteralExpr(SourceLocation(), ArgTy, 0, false);
Chris Lattner0d9bcea2007-08-30 17:45:32 +00002241
Chris Lattnerb37522e2007-08-31 21:49:13 +00002242 // offsetof with non-identifier designators (e.g. "offsetof(x, a.b[c])") are a
2243 // GCC extension, diagnose them.
2244 if (NumComponents != 1)
2245 Diag(BuiltinLoc, diag::ext_offsetof_extended_field_designator,
2246 SourceRange(CompPtr[1].LocStart, CompPtr[NumComponents-1].LocEnd));
2247
Chris Lattner0d9bcea2007-08-30 17:45:32 +00002248 for (unsigned i = 0; i != NumComponents; ++i) {
2249 const OffsetOfComponent &OC = CompPtr[i];
2250 if (OC.isBrackets) {
2251 // Offset of an array sub-field. TODO: Should we allow vector elements?
2252 const ArrayType *AT = Res->getType()->getAsArrayType();
2253 if (!AT) {
2254 delete Res;
2255 return Diag(OC.LocEnd, diag::err_offsetof_array_type,
2256 Res->getType().getAsString());
2257 }
2258
Chris Lattner2af6a802007-08-30 17:59:59 +00002259 // FIXME: C++: Verify that operator[] isn't overloaded.
2260
Chris Lattner0d9bcea2007-08-30 17:45:32 +00002261 // C99 6.5.2.1p1
2262 Expr *Idx = static_cast<Expr*>(OC.U.E);
2263 if (!Idx->getType()->isIntegerType())
2264 return Diag(Idx->getLocStart(), diag::err_typecheck_subscript,
2265 Idx->getSourceRange());
2266
2267 Res = new ArraySubscriptExpr(Res, Idx, AT->getElementType(), OC.LocEnd);
2268 continue;
2269 }
2270
2271 const RecordType *RC = Res->getType()->getAsRecordType();
2272 if (!RC) {
2273 delete Res;
2274 return Diag(OC.LocEnd, diag::err_offsetof_record_type,
2275 Res->getType().getAsString());
2276 }
2277
2278 // Get the decl corresponding to this.
2279 RecordDecl *RD = RC->getDecl();
2280 FieldDecl *MemberDecl = RD->getMember(OC.U.IdentInfo);
2281 if (!MemberDecl)
2282 return Diag(BuiltinLoc, diag::err_typecheck_no_member,
2283 OC.U.IdentInfo->getName(),
2284 SourceRange(OC.LocStart, OC.LocEnd));
Chris Lattner2af6a802007-08-30 17:59:59 +00002285
2286 // FIXME: C++: Verify that MemberDecl isn't a static field.
2287 // FIXME: Verify that MemberDecl isn't a bitfield.
Eli Friedman76b49832008-02-06 22:48:16 +00002288 // MemberDecl->getType() doesn't get the right qualifiers, but it doesn't
2289 // matter here.
2290 Res = new MemberExpr(Res, false, MemberDecl, OC.LocEnd, MemberDecl->getType());
Chris Lattner0d9bcea2007-08-30 17:45:32 +00002291 }
2292
2293 return new UnaryOperator(Res, UnaryOperator::OffsetOf, Context.getSizeType(),
2294 BuiltinLoc);
2295}
2296
2297
Steve Naroff5cbb02f2007-09-16 14:56:35 +00002298Sema::ExprResult Sema::ActOnTypesCompatibleExpr(SourceLocation BuiltinLoc,
Steve Naroff63bad2d2007-08-01 22:05:33 +00002299 TypeTy *arg1, TypeTy *arg2,
2300 SourceLocation RPLoc) {
2301 QualType argT1 = QualType::getFromOpaquePtr(arg1);
2302 QualType argT2 = QualType::getFromOpaquePtr(arg2);
2303
2304 assert((!argT1.isNull() && !argT2.isNull()) && "Missing type argument(s)");
2305
Chris Lattner0d9bcea2007-08-30 17:45:32 +00002306 return new TypesCompatibleExpr(Context.IntTy, BuiltinLoc, argT1, argT2,RPLoc);
Steve Naroff63bad2d2007-08-01 22:05:33 +00002307}
2308
Steve Naroff5cbb02f2007-09-16 14:56:35 +00002309Sema::ExprResult Sema::ActOnChooseExpr(SourceLocation BuiltinLoc, ExprTy *cond,
Steve Naroff93c53012007-08-03 21:21:27 +00002310 ExprTy *expr1, ExprTy *expr2,
2311 SourceLocation RPLoc) {
2312 Expr *CondExpr = static_cast<Expr*>(cond);
2313 Expr *LHSExpr = static_cast<Expr*>(expr1);
2314 Expr *RHSExpr = static_cast<Expr*>(expr2);
2315
2316 assert((CondExpr && LHSExpr && RHSExpr) && "Missing type argument(s)");
2317
2318 // The conditional expression is required to be a constant expression.
2319 llvm::APSInt condEval(32);
2320 SourceLocation ExpLoc;
2321 if (!CondExpr->isIntegerConstantExpr(condEval, Context, &ExpLoc))
2322 return Diag(ExpLoc, diag::err_typecheck_choose_expr_requires_constant,
2323 CondExpr->getSourceRange());
2324
2325 // If the condition is > zero, then the AST type is the same as the LSHExpr.
2326 QualType resType = condEval.getZExtValue() ? LHSExpr->getType() :
2327 RHSExpr->getType();
2328 return new ChooseExpr(BuiltinLoc, CondExpr, LHSExpr, RHSExpr, resType, RPLoc);
2329}
2330
Nate Begemanbd881ef2008-01-30 20:50:20 +00002331/// ExprsMatchFnType - return true if the Exprs in array Args have
Nate Begeman9f3bfb72008-01-17 17:46:27 +00002332/// QualTypes that match the QualTypes of the arguments of the FnType.
Nate Begemanbd881ef2008-01-30 20:50:20 +00002333/// The number of arguments has already been validated to match the number of
2334/// arguments in FnType.
2335static bool ExprsMatchFnType(Expr **Args, const FunctionTypeProto *FnType) {
Nate Begeman9f3bfb72008-01-17 17:46:27 +00002336 unsigned NumParams = FnType->getNumArgs();
Nate Begeman778fd3b2008-04-18 23:35:14 +00002337 for (unsigned i = 0; i != NumParams; ++i) {
2338 QualType ExprTy = Args[i]->getType().getCanonicalType();
2339 QualType ParmTy = FnType->getArgType(i).getCanonicalType();
2340
2341 if (ExprTy.getUnqualifiedType() != ParmTy.getUnqualifiedType())
Nate Begeman9f3bfb72008-01-17 17:46:27 +00002342 return false;
Nate Begeman778fd3b2008-04-18 23:35:14 +00002343 }
Nate Begeman9f3bfb72008-01-17 17:46:27 +00002344 return true;
2345}
2346
2347Sema::ExprResult Sema::ActOnOverloadExpr(ExprTy **args, unsigned NumArgs,
2348 SourceLocation *CommaLocs,
2349 SourceLocation BuiltinLoc,
2350 SourceLocation RParenLoc) {
Nate Begemanc6078c92008-01-31 05:38:29 +00002351 // __builtin_overload requires at least 2 arguments
2352 if (NumArgs < 2)
2353 return Diag(RParenLoc, diag::err_typecheck_call_too_few_args,
2354 SourceRange(BuiltinLoc, RParenLoc));
Nate Begeman9f3bfb72008-01-17 17:46:27 +00002355
Nate Begeman9f3bfb72008-01-17 17:46:27 +00002356 // The first argument is required to be a constant expression. It tells us
2357 // the number of arguments to pass to each of the functions to be overloaded.
Nate Begemanc6078c92008-01-31 05:38:29 +00002358 Expr **Args = reinterpret_cast<Expr**>(args);
Nate Begeman9f3bfb72008-01-17 17:46:27 +00002359 Expr *NParamsExpr = Args[0];
2360 llvm::APSInt constEval(32);
2361 SourceLocation ExpLoc;
2362 if (!NParamsExpr->isIntegerConstantExpr(constEval, Context, &ExpLoc))
2363 return Diag(ExpLoc, diag::err_overload_expr_requires_non_zero_constant,
2364 NParamsExpr->getSourceRange());
2365
2366 // Verify that the number of parameters is > 0
2367 unsigned NumParams = constEval.getZExtValue();
2368 if (NumParams == 0)
2369 return Diag(ExpLoc, diag::err_overload_expr_requires_non_zero_constant,
2370 NParamsExpr->getSourceRange());
2371 // Verify that we have at least 1 + NumParams arguments to the builtin.
2372 if ((NumParams + 1) > NumArgs)
2373 return Diag(RParenLoc, diag::err_typecheck_call_too_few_args,
2374 SourceRange(BuiltinLoc, RParenLoc));
2375
2376 // Figure out the return type, by matching the args to one of the functions
Nate Begemanbd881ef2008-01-30 20:50:20 +00002377 // listed after the parameters.
Nate Begemanc6078c92008-01-31 05:38:29 +00002378 OverloadExpr *OE = 0;
Nate Begeman9f3bfb72008-01-17 17:46:27 +00002379 for (unsigned i = NumParams + 1; i < NumArgs; ++i) {
2380 // UsualUnaryConversions will convert the function DeclRefExpr into a
2381 // pointer to function.
2382 Expr *Fn = UsualUnaryConversions(Args[i]);
2383 FunctionTypeProto *FnType = 0;
Nate Begemanbd881ef2008-01-30 20:50:20 +00002384 if (const PointerType *PT = Fn->getType()->getAsPointerType()) {
2385 QualType PointeeType = PT->getPointeeType().getCanonicalType();
2386 FnType = dyn_cast<FunctionTypeProto>(PointeeType);
2387 }
Nate Begeman9f3bfb72008-01-17 17:46:27 +00002388
2389 // The Expr type must be FunctionTypeProto, since FunctionTypeProto has no
2390 // parameters, and the number of parameters must match the value passed to
2391 // the builtin.
2392 if (!FnType || (FnType->getNumArgs() != NumParams))
Nate Begemanbd881ef2008-01-30 20:50:20 +00002393 return Diag(Fn->getExprLoc(), diag::err_overload_incorrect_fntype,
2394 Fn->getSourceRange());
Nate Begeman9f3bfb72008-01-17 17:46:27 +00002395
2396 // Scan the parameter list for the FunctionType, checking the QualType of
Nate Begemanbd881ef2008-01-30 20:50:20 +00002397 // each parameter against the QualTypes of the arguments to the builtin.
Nate Begeman9f3bfb72008-01-17 17:46:27 +00002398 // If they match, return a new OverloadExpr.
Nate Begemanc6078c92008-01-31 05:38:29 +00002399 if (ExprsMatchFnType(Args+1, FnType)) {
2400 if (OE)
2401 return Diag(Fn->getExprLoc(), diag::err_overload_multiple_match,
2402 OE->getFn()->getSourceRange());
2403 // Remember our match, and continue processing the remaining arguments
2404 // to catch any errors.
2405 OE = new OverloadExpr(Args, NumArgs, i, FnType->getResultType(),
2406 BuiltinLoc, RParenLoc);
2407 }
Nate Begeman9f3bfb72008-01-17 17:46:27 +00002408 }
Nate Begemanc6078c92008-01-31 05:38:29 +00002409 // Return the newly created OverloadExpr node, if we succeded in matching
2410 // exactly one of the candidate functions.
2411 if (OE)
2412 return OE;
Nate Begeman9f3bfb72008-01-17 17:46:27 +00002413
2414 // If we didn't find a matching function Expr in the __builtin_overload list
2415 // the return an error.
2416 std::string typeNames;
Nate Begemanbd881ef2008-01-30 20:50:20 +00002417 for (unsigned i = 0; i != NumParams; ++i) {
2418 if (i != 0) typeNames += ", ";
2419 typeNames += Args[i+1]->getType().getAsString();
2420 }
Nate Begeman9f3bfb72008-01-17 17:46:27 +00002421
2422 return Diag(BuiltinLoc, diag::err_overload_no_match, typeNames,
2423 SourceRange(BuiltinLoc, RParenLoc));
2424}
2425
Anders Carlsson36760332007-10-15 20:28:48 +00002426Sema::ExprResult Sema::ActOnVAArg(SourceLocation BuiltinLoc,
2427 ExprTy *expr, TypeTy *type,
Chris Lattner005ed752008-01-04 18:04:52 +00002428 SourceLocation RPLoc) {
Anders Carlsson36760332007-10-15 20:28:48 +00002429 Expr *E = static_cast<Expr*>(expr);
2430 QualType T = QualType::getFromOpaquePtr(type);
2431
2432 InitBuiltinVaListType();
2433
Chris Lattner005ed752008-01-04 18:04:52 +00002434 if (CheckAssignmentConstraints(Context.getBuiltinVaListType(), E->getType())
2435 != Compatible)
Anders Carlsson36760332007-10-15 20:28:48 +00002436 return Diag(E->getLocStart(),
2437 diag::err_first_argument_to_va_arg_not_of_type_va_list,
2438 E->getType().getAsString(),
2439 E->getSourceRange());
2440
2441 // FIXME: Warn if a non-POD type is passed in.
2442
2443 return new VAArgExpr(BuiltinLoc, E, T, RPLoc);
2444}
2445
Chris Lattner005ed752008-01-04 18:04:52 +00002446bool Sema::DiagnoseAssignmentResult(AssignConvertType ConvTy,
2447 SourceLocation Loc,
2448 QualType DstType, QualType SrcType,
2449 Expr *SrcExpr, const char *Flavor) {
2450 // Decode the result (notice that AST's are still created for extensions).
2451 bool isInvalid = false;
2452 unsigned DiagKind;
2453 switch (ConvTy) {
2454 default: assert(0 && "Unknown conversion type");
2455 case Compatible: return false;
Chris Lattnerd951b7b2008-01-04 18:22:42 +00002456 case PointerToInt:
Chris Lattner005ed752008-01-04 18:04:52 +00002457 DiagKind = diag::ext_typecheck_convert_pointer_int;
2458 break;
Chris Lattnerd951b7b2008-01-04 18:22:42 +00002459 case IntToPointer:
2460 DiagKind = diag::ext_typecheck_convert_int_pointer;
2461 break;
Chris Lattner005ed752008-01-04 18:04:52 +00002462 case IncompatiblePointer:
2463 DiagKind = diag::ext_typecheck_convert_incompatible_pointer;
2464 break;
2465 case FunctionVoidPointer:
2466 DiagKind = diag::ext_typecheck_convert_pointer_void_func;
2467 break;
2468 case CompatiblePointerDiscardsQualifiers:
2469 DiagKind = diag::ext_typecheck_convert_discards_qualifiers;
2470 break;
2471 case Incompatible:
2472 DiagKind = diag::err_typecheck_convert_incompatible;
2473 isInvalid = true;
2474 break;
2475 }
2476
2477 Diag(Loc, DiagKind, DstType.getAsString(), SrcType.getAsString(), Flavor,
2478 SrcExpr->getSourceRange());
2479 return isInvalid;
2480}